Free Electron
SurfaceSphere.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __surface_SurfaceSphere_h__
8 #define __surface_SurfaceSphere_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief Spherical Surface
17 
18  @ingroup surface
19 *//***************************************************************************/
20 class FE_DL_EXPORT SurfaceSphere:
21  public SurfaceBase,
22  public CastableAs<SurfaceSphere>
23 {
24  protected:
25 
26  class Impact:
27  public SurfaceBase::Impact,
28  public CastableAs<Impact>
29  {
30  public:
31  Impact(void):
32  m_resolved(FALSE)
33  {
34 #if FE_COUNTED_STORE_TRACKER
35  setName("SurfaceSphere::Impact");
36 #endif
37  }
38  virtual ~Impact(void) {}
39 
40  virtual void reset(void)
41  {
42  m_resolved=FALSE;
43  }
44 
45  virtual SpatialVector intersection(void)
46  {
47  if(!m_resolved)
48  {
49  resolve();
50  }
51  return transformVector(m_transform,
52  m_intersection);
53  }
54  SpatialVector intersectionLocal(void)
55  {
56  if(!m_resolved)
57  {
58  resolve();
59  }
60  return m_intersection;
61  }
62  virtual void setIntersectionLocal(
63  SpatialVector a_intersection)
64  { m_intersection=a_intersection; }
65 
66  virtual SpatialVector normal(void)
67  {
68  if(!m_resolved)
69  {
70  resolve();
71  }
72  return rotateVector(m_transform,m_normal);
73  }
74  void setNormalLocal(SpatialVector a_normal)
75  { m_normal=a_normal; }
76 
77  protected:
78 
79  virtual void resolve(void)
80  {
82  spSurfaceSphere(m_hpSurface);
83  if(spSurfaceSphere.isValid())
84  {
85  spSurfaceSphere->resolveImpact(
86  sp<ImpactI>(this));
87  m_resolved=TRUE;
88  }
89  }
90 
91  SpatialVector m_intersection;
92  SpatialVector m_normal;
93  BWORD m_resolved;
94  };
95 
96  public:
97  SurfaceSphere(void);
98 virtual ~SurfaceSphere(void) {}
99 
100  //* As Protectable
101 virtual Protectable* clone(Protectable* pInstance=NULL);
102 
103  //* As SurfaceI
104 virtual SpatialVector center(void)
105  { checkCache();
106  return m_center; }
107 virtual SpatialVector center(void) const
108  { return m_center; }
109 virtual Real radius(void)
110  { checkCache();
111  return m_radius; }
112 virtual Real radius(void) const
113  { return m_radius; }
114 virtual Color diffuse(void) const { return m_diffuse; }
115 
116  using SurfaceBase::sample;
117 
118 virtual SpatialTransform sample(Vector2 a_uv) const;
119 
120  using SurfaceBase::nearestPoint;
121 
122 virtual sp<ImpactI> nearestPoint(const SpatialVector& a_origin,
123  Real a_maxDistance) const
124  {
125  SpatialVector direction;
126  Real distance=
128  m_center,m_radius,a_origin,direction);
129  if(a_maxDistance>0.0 && distance>a_maxDistance)
130  {
131  return sp<Impact>(NULL);
132  }
133 
134  sp<Impact> spImpact=m_sphereImpactPool.get();
135  FEASSERT(spImpact.isValid());
136 
137  spImpact->setSurface(this);
138  spImpact->setLocationLocal(m_center);
139  spImpact->setDiffuse(m_diffuse);
140  spImpact->setRadius(m_radius);
141  spImpact->setOrigin(a_origin);
142  spImpact->setDirection(direction);
143  spImpact->setDistance(distance);
144 
145  return spImpact;
146  }
147 
148  using SurfaceBase::rayImpact;
149 
150 virtual sp<ImpactI> rayImpact(const SpatialVector& a_origin,
151  const SpatialVector& a_direction,
152  Real a_maxDistance,BWORD a_anyHit) const
153  {
154  const Real distance=
156  m_center,m_radius,
157  a_origin,a_direction);
158 
159  if(distance<=0.0 ||
160  (a_maxDistance>0.0 &&
161  distance>a_maxDistance))
162  {
163  return sp<Impact>(NULL);
164  }
165 
166  sp<Impact> spImpact=m_sphereImpactPool.get();
167  FEASSERT(spImpact.isValid());
168 
169  spImpact->setSurface(this);
170  spImpact->setLocationLocal(m_center);
171  spImpact->setDiffuse(m_diffuse);
172  spImpact->setRadius(m_radius);
173  spImpact->setOrigin(a_origin);
174  spImpact->setDirection(a_direction);
175  spImpact->setDistance(distance);
176  return spImpact;
177  }
178 
179  using SurfaceBase::draw;
180 
181 virtual void draw(const SpatialTransform&,sp<DrawI> a_spDrawI,
182  const fe::Color* a_color,
183  sp<DrawBufferI> a_spDrawBuffer,
184  sp<PartitionI> a_spPartition) const;
185 
186  using SurfaceBase::bind;
187 
188  //* as RecordableI
189 virtual void bind(Record& rRecord);
190 
191  void setRadius(Real a_radius)
192  { m_radius=a_radius; }
193  void setCenter(const SpatialVector& a_rCenter)
194  { m_center=a_rCenter; }
195 
196  protected:
197 
198 virtual void cache(void);
199 virtual void resolveImpact(sp<ImpactI> a_spImpactI) const
200  {
201  sp<Impact> spImpact(a_spImpactI);
202 
203  SpatialVector intersection;
204  SpatialVector normal;
206  spImpact->locationLocal(),
207  spImpact->radius(),
208  spImpact->origin(),
209  spImpact->direction(),
210  spImpact->distance(),
211  intersection,normal);
212  spImpact->setIntersectionLocal(intersection);
213  spImpact->setNormalLocal(normal);
214  }
215 
216  WeakRecord m_record;
217 
218  SpatialVector m_center;
219  Real m_radius;
220 
221  Color m_emissive;
222  Color m_ambient;
223  Color m_diffuse;
224  Color m_specular;
225  Real m_shininess;
226 
227 mutable CountedPool<Impact> m_sphereImpactPool;
228 };
229 
230 } /* namespace ext */
231 } /* namespace fe */
232 
233 #endif /* __surface_SurfaceSphere_h__ */
234 
virtual void draw(sp< DrawI > a_spDrawI, const fe::Color *a_pColor)
Draw with current settings.
Definition: SurfaceBase.h:590
Base class providing protection counting for cp<>
Definition: Protectable.h:28
kernel
Definition: namespace.dox:3
Special vector for color (RGBA)
Definition: Color.h:21
virtual void bind(Instance a_instance)
associate with model data
Definition: SurfaceBase.h:175
Find nearest point on sphere.
Definition: PointSphereNearest.h:21
virtual Color diffuse(void) const
Return the diffuse color.
Definition: SurfaceSphere.h:114
Spherical Surface.
Definition: SurfaceSphere.h:20
Find intersection between ray and sphere.
Definition: RaySphereIntersect.h:21
Trivial Point Origin Surface.
Definition: SurfaceBase.h:20
Reference to an instance of a Layout.
Definition: RecordSB.h:35
Per-class participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:192
Non-persistent reference to an instance of a Layout.
Definition: WeakRecordSB.h:17