Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/objMesh/triangle.h

raw symbols SCIP
1 /*************************************************************************
2 * *
3 * Vega FEM Simulation Library Version 3.1 *
4 * *
5 * "objMesh" library , Copyright (C) 2007 CMU, 2009 MIT, 2016 USC *
6 * All rights reserved. *
7 * *
8 * Code authors: Jernej Barbic, Christopher Twigg, Daniel Schroeder *
9 * http://www.jernejbarbic.com/code *
10 * *
11 * Research: Jernej Barbic, Fun Shing Sin, Daniel Schroeder, *
12 * Doug L. James, Jovan Popovic *
13 * *
14 * Funding: National Science Foundation, Link Foundation, *
15 * Singapore-MIT GAMBIT Game Lab, *
16 * Zumberge Research and Innovation Fund at USC *
17 * *
18 * This library is free software; you can redistribute it and/or *
19 * modify it under the terms of the BSD-style license that is *
20 * included with this library in the file LICENSE.txt *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
25 * LICENSE.TXT for more details. *
26 * *
27 *************************************************************************/
28
29 /*
30 A triangle
31 Jernej Barbic, CMU
32 */
33
34 #ifndef _TRIANGLE_H_
35 #define _TRIANGLE_H_
36
37 #include <math.h>
38 #include <vector>
39 #include <iostream>
40 #include "boundingBox.h"
41 #include "minivector.h"
42
43 class TriangleBasic
44 {
45 public:
46
47 TriangleBasic(const Vec3d & first_g, const Vec3d & second_g, const Vec3d & third_g):
48 first_(first_g), second_(second_g), third_(third_g), index_(0) {}
49
50 // accessors
51 inline const Vec3d & first() const {return first_ ;}
52 inline const Vec3d & second() const {return second_ ;}
53 inline const Vec3d & third() const {return third_ ;}
54 inline int index() const { return index_; }
55
56 // index can be used to keep track of what triangle this is
57 inline void setIndex(int index_) { this->index_ = index_; }
58
59 // squared 3d distance to a point
60 double distanceToPoint2(const Vec3d & point) const { std::cout << "Unimplemented..." << std::endl; return 1;} // unimplemented (it is implemented in class "TriangleWithCollisionInfo" below)
61 double distanceToPoint(const Vec3d & point) const { return sqrt(distanceToPoint2(point));}
62
63 bool doesIntersectBox(const BoundingBox & bbox) const;
64
65 // int lineSegmentIntersection(Vec3d segmentStart, Vec3d segmentEnd, Vec3d * intersectionPoint);
66 int lineSegmentIntersection(const Vec3d & segmentStart, const Vec3d & segmentEnd, Vec3d * intersectionPoint) const;
67 // Output: intersection point (when it exists)
68 // Return: -1 = triangle is degenerate (a segment or point)
69 // 0 = disjoint (no intersect)
70 // 1 = intersect in unique point I1
71 // 2 = are in the same plane
72
73
74 void render() const;
75 void renderEdges() const;
76
77 Vec3d getBarycentricLocation(double alpha, double beta, double gamma) const { return alpha * first_ + beta * second_ + gamma * third_; }
78
79 protected:
80
81 Vec3d first_, second_, third_;
82 int index_;
83 };
84
85 // makes the triangle list unique, using the "index_" field of the <TriangleClass>
86 template<class TriangleClass>
87 void makeUniqueList(const std::vector<TriangleClass*> & triangleList, std::vector<TriangleClass*> & uniqueList);
88
89 template<class TriangleClass>
90 void makeUniqueList(std::vector<TriangleClass*> & triangleList); // overwrites triangleList with the unique list
91
92 class TriangleWithCollisionInfo : public TriangleBasic
93 {
94 public:
95 TriangleWithCollisionInfo(const Vec3d & first_g, const Vec3d & second_g, const Vec3d & third_g):
96 TriangleBasic(first_g,second_g,third_g) { ComputeCollisionInfo(); }
97
98 // squared 3d distance to a point
99 // also returns the closest feature to the query point:
100 // 0: vertex0
101 // 1: vertex1
102 // 2: vertex2
103 // 3: edge among 01
104 // 4: edge among 12
105 // 5: edge among 20
106 // 6: the face itself
107 double distanceToPoint2(const Vec3d & point, int * closestFeature) const;
108 double distanceToPoint2(const Vec3d & point, int * closestFeature, double * alpha, double * beta, double * gamma) const; // also returns the barycentric coordinates of the closest point
109
110 protected:
111
112 // note: the following collision detection parameters are pre-computed with respect to a permuted set of triangle indices (a cycle)
113 Mat3d Q;
114 Vec3d x0;
115
116 double sidea,sideb,sidec,area;
117 Vec3d S1,S2,N11,N12,N21,N22;
118
119 int permutation[6]; // used internally so that first triangle edge is always the longest
120 // transformation(x) equals Q * x + x0;
121
122 void ComputeCollisionInfo();
123 };
124
125 class TriangleWithCollisionInfoAndPseudoNormals : public TriangleWithCollisionInfo
126 {
127 public:
128 // pseudoNormals is a caller-provided list of 7 normals, indexed the same as closest features below
129 TriangleWithCollisionInfoAndPseudoNormals(const Vec3d & first_g, const Vec3d & second_g, const Vec3d & third_g, const Vec3d pseudoNormals[7]);
130
131 inline const Vec3d & pseudoNormal(int closestFeature) const { return pseudoNormal_[closestFeature]; }
132 inline Vec3d interpolatedVertexPseudoNormal(double alpha, double beta, double gamma) const { return norm(alpha * pseudoNormal_[0] + beta * pseudoNormal_[1] + gamma * pseudoNormal_[2]); }
133
134 // for vertices, returns the vertex itself
135 // for edges, returns the midpoint of the edge (for the purposes of distance sign test, this could be any point on the edge)
136 // for faces, returns the face centroid
137 inline const Vec3d & pseudoClosestPosition(int closestFeature) const { return pseudoClosestPosition_[closestFeature]; }
138
139 protected:
140 Vec3d pseudoNormal_[7];
141 Vec3d pseudoClosestPosition_[7];
142
143 };
144
145 #endif
146