Cortex  10.0.0-a4
MeshPrimitiveEvaluator.h
1 //
3 // Copyright (c) 2008-2011, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_MESHPRIMITIVEEVALUATOR_H
36 #define IE_CORE_MESHPRIMITIVEEVALUATOR_H
37 
38 #include <vector>
39 
40 #include "tbb/mutex.h"
41 
42 #include "IECore/Export.h"
43 #include "IECore/PrimitiveEvaluator.h"
44 #include "IECore/MeshPrimitive.h"
45 #include "IECore/BoundedKDTree.h"
46 
47 namespace IECore
48 {
49 
52 class IECORE_API MeshPrimitiveEvaluator : public PrimitiveEvaluator
53 {
54  public:
55 
57 
58  IE_CORE_DECLARERUNTIMETYPED( MeshPrimitiveEvaluator, PrimitiveEvaluator );
59 
60  class IECORE_API Result : public PrimitiveEvaluator::Result
61  {
62  friend class MeshPrimitiveEvaluator;
63 
64  public:
65 
66  IE_CORE_DECLAREMEMBERPTR( Result );
67 
68  Result();
69 
70  Imath::V3f point() const override;
71  Imath::V3f normal() const override;
72  Imath::V2f uv() const override;
73  Imath::V3f uTangent() const override;
74  Imath::V3f vTangent() const override;
75 
76  Imath::V3f vectorPrimVar( const PrimitiveVariable &pv ) const override;
77  Imath::V2f vec2PrimVar( const PrimitiveVariable &pv ) const override;
78  float floatPrimVar( const PrimitiveVariable &pv ) const override;
79  int intPrimVar( const PrimitiveVariable &pv ) const override;
80  const std::string &stringPrimVar( const PrimitiveVariable &pv ) const override;
81  Imath::Color3f colorPrimVar( const PrimitiveVariable &pv ) const override;
82  half halfPrimVar( const PrimitiveVariable &pv ) const override;
83 
84  unsigned int triangleIndex() const;
85  const Imath::V3f &barycentricCoordinates() const;
86  const Imath::V3i &vertexIds() const;
87 
88  protected:
89 
90  Imath::V3i m_vertexIds;
91  Imath::V3f m_bary;
94  Imath::V3f m_p;
95  Imath::V3f m_n;
96  Imath::V2f m_uv;
97  unsigned int m_triangleIdx;
98 
99  template<typename T>
100  T getPrimVar( const PrimitiveVariable &pv ) const;
101 
102 
103  };
104  IE_CORE_DECLAREPTR( Result );
105 
106  static PrimitiveEvaluatorPtr create( ConstPrimitivePtr primitive );
107 
108  MeshPrimitiveEvaluator( ConstMeshPrimitivePtr mesh );
109 
110  ~MeshPrimitiveEvaluator() override;
111 
112  ConstPrimitivePtr primitive() const override;
113  MeshPrimitive::ConstPtr mesh() const;
114 
115  PrimitiveEvaluator::ResultPtr createResult() const override;
116 
117  void validateResult( PrimitiveEvaluator::Result *result ) const override;
118 
119  bool closestPoint( const Imath::V3f &p, PrimitiveEvaluator::Result *result ) const override;
120 
121  bool pointAtUV( const Imath::V2f &uv, PrimitiveEvaluator::Result *result ) const override;
122 
123  bool intersectionPoint( const Imath::V3f &origin, const Imath::V3f &direction,
124  PrimitiveEvaluator::Result *result, float maxDistance = Imath::limits<float>::max() ) const override;
125 
126  int intersectionPoints( const Imath::V3f &origin, const Imath::V3f &direction,
127  std::vector<PrimitiveEvaluator::ResultPtr> &results, float maxDistance = Imath::limits<float>::max() ) const override;
128 
130  bool barycentricPosition( unsigned int triangleIndex, const Imath::V3f &barycentricCoordinates, PrimitiveEvaluator::Result *result ) const;
131 
132  bool signedDistance( const Imath::V3f &p, float &distance, PrimitiveEvaluator::Result *result ) const override;
133 
134  float volume() const override;
135 
136  Imath::V3f centerOfGravity() const override;
137 
138  float surfaceArea() const override;
139 
140 
142  const Imath::Box2f uvBound() const;
143 
149 
150  typedef Imath::Box3f TriangleBound;
153  typedef std::vector<TriangleBound> TriangleBoundVector;
157  const TriangleBoundVector *triangleBounds() const;
160  const TriangleBoundTree *triangleBoundTree() const;
161 
163  typedef Imath::Box2f UVBound;
165  typedef std::vector<UVBound> UVBoundVector;
170  const UVBoundVector *uvBounds() const;
174  const UVBoundTree *uvBoundTree() const;
176 
177  protected:
178 
179  ConstMeshPrimitivePtr m_mesh;
180  ConstV3fVectorDataPtr m_verts;
181  const std::vector<int> *m_meshVertexIds;
182 
183  TriangleBoundVector m_triangles;
184  TriangleBoundTree *m_tree;
185 
186  UVBoundVector m_uvTriangles;
187  UVBoundTree *m_uvTree;
188 
189  bool pointAtUVWalk( UVBoundTree::NodeIndex nodeIndex, const Imath::V2f &targetUV, Result *result ) const;
190  void closestPointWalk( TriangleBoundTree::NodeIndex nodeIndex, const Imath::V3f &p, float &closestDistanceSqrd, Result *result ) const;
191  bool intersectionPointWalk( TriangleBoundTree::NodeIndex nodeIndex, const Imath::Line3f &ray, float &maxDistSqrd, Result *result, bool &hit ) const;
192  void intersectionPointsWalk( TriangleBoundTree::NodeIndex nodeIndex, const Imath::Line3f &ray, float maxDistSqrd, std::vector<PrimitiveEvaluator::ResultPtr> &results ) const;
193 
194  void calculateMassProperties() const;
195  void calculateAverageNormals() const;
196 
197  void triangleUVs( size_t triangleIndex, const Imath::V3i &vertexIds, Imath::V2f uv[3] ) const;
198  PrimitiveVariable m_uv;
199 
200  mutable bool m_haveMassProperties;
201  mutable float m_volume;
202  mutable Imath::V3f m_centerOfGravity;
203  mutable Imath::M33f m_inertia;
204 
205  mutable bool m_haveSurfaceArea;
206  mutable float m_surfaceArea;
207 
208  typedef tbb::mutex NormalsMutex;
209  mutable NormalsMutex m_normalsMutex;
210  mutable bool m_haveAverageNormals;
211  typedef int VertexIndex;
212  typedef int TriangleIndex;
213  typedef std::pair<VertexIndex, VertexIndex> Edge;
214 
215  typedef std::map< Edge, Imath::V3f > EdgeAverageNormals;
216  mutable EdgeAverageNormals m_edgeAverageNormals;
217 
218  mutable V3fVectorDataPtr m_vertexAngleWeightedNormals;
219 
220 };
221 
222 IE_CORE_DECLAREPTR( MeshPrimitiveEvaluator );
223 
224 } // namespace IECore
225 
226 #endif // IE_CORE_MESHPRIMITIVEEVALUATOR_H
Imath::Box2f UVBound
A type for storing the uv bounding box for a triangle.
Definition: MeshPrimitiveEvaluator.h:163
Definition: BoundedKDTree.h:50
Definition: PrimitiveVariable.h:47
Definition: PrimitiveEvaluator.h:57
Definition: MeshPrimitive.h:56
Definition: MeshPrimitiveEvaluator.h:52
std::vector< UVBound > UVBoundVector
A type for storing an array of uv bounds, one per triangle.
Definition: MeshPrimitiveEvaluator.h:165
Definition: PrimitiveEvaluator.h:68
std::vector< TriangleBound > TriangleBoundVector
A type for storing an array of bounding boxes, one per triangle.
Definition: MeshPrimitiveEvaluator.h:153
BoundedKDTree< TriangleBoundVector::iterator > TriangleBoundTree
A BoundedKDTree providing accelerated lookups of triangles using their bounding boxes.
Definition: MeshPrimitiveEvaluator.h:155
Imath::Box3f TriangleBound
A type for storing the bounding box for a triangle.
Definition: MeshPrimitiveEvaluator.h:151
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43
BoundedKDTree< UVBoundVector::iterator > UVBoundTree
A BoundedKDTree providing accelerated lookups of triangles using their uv bounds. ...
Definition: MeshPrimitiveEvaluator.h:167