Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MAT.inl
1 /*
2 Copyright (c) 2007, Michael Kazhdan
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12 
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
29 // MinimalAreaTriangulation //
31 template <class Real>
33 {
34  bestTriangulation=NULL;
35  midPoint=NULL;
36 }
37 template <class Real>
39 {
40  if(bestTriangulation)
41  delete[] bestTriangulation;
42  bestTriangulation=NULL;
43  if(midPoint)
44  delete[] midPoint;
45  midPoint=NULL;
46 }
47 template <class Real>
48 void MinimalAreaTriangulation<Real>::GetTriangulation(const std::vector<Point3D<Real> >& vertices,std::vector<TriangleIndex>& triangles)
49 {
50  if(vertices.size()==3)
51  {
52  triangles.resize(1);
53  triangles[0].idx[0]=0;
54  triangles[0].idx[1]=1;
55  triangles[0].idx[2]=2;
56  return;
57  }
58  else if(vertices.size()==4)
59  {
60  TriangleIndex tIndex[2][2];
61  Real area[2];
62 
63  area[0]=area[1]=0;
64  triangles.resize(2);
65 
66  tIndex[0][0].idx[0]=0;
67  tIndex[0][0].idx[1]=1;
68  tIndex[0][0].idx[2]=2;
69  tIndex[0][1].idx[0]=2;
70  tIndex[0][1].idx[1]=3;
71  tIndex[0][1].idx[2]=0;
72 
73  tIndex[1][0].idx[0]=0;
74  tIndex[1][0].idx[1]=1;
75  tIndex[1][0].idx[2]=3;
76  tIndex[1][1].idx[0]=3;
77  tIndex[1][1].idx[1]=1;
78  tIndex[1][1].idx[2]=2;
79 
80  Point3D<Real> n,p1,p2;
81  for(int i=0;i<2;i++)
82  for(int j=0;j<2;j++)
83  {
84  p1=vertices[tIndex[i][j].idx[1]]-vertices[tIndex[i][j].idx[0]];
85  p2=vertices[tIndex[i][j].idx[2]]-vertices[tIndex[i][j].idx[0]];
86  CrossProduct(p1,p2,n);
87  area[i] += Real( Length(n) );
88  }
89  if(area[0]>area[1])
90  {
91  triangles[0]=tIndex[1][0];
92  triangles[1]=tIndex[1][1];
93  }
94  else
95  {
96  triangles[0]=tIndex[0][0];
97  triangles[1]=tIndex[0][1];
98  }
99  return;
100  }
101  if(bestTriangulation)
102  delete[] bestTriangulation;
103  if(midPoint)
104  delete[] midPoint;
105  bestTriangulation=NULL;
106  midPoint=NULL;
107  size_t eCount=vertices.size();
108  bestTriangulation=new Real[eCount*eCount];
109  midPoint=new int[eCount*eCount];
110  for(size_t i=0;i<eCount*eCount;i++)
111  bestTriangulation[i]=-1;
112  memset(midPoint,-1,sizeof(int)*eCount*eCount);
113  GetArea(0,1,vertices);
114  triangles.clear();
115  GetTriangulation(0,1,vertices,triangles);
116 }
117 template <class Real>
118 Real MinimalAreaTriangulation<Real>::GetArea(const std::vector<Point3D<Real> >& vertices)
119 {
120  if(bestTriangulation)
121  delete[] bestTriangulation;
122  if(midPoint)
123  delete[] midPoint;
124  bestTriangulation=NULL;
125  midPoint=NULL;
126  int eCount=vertices.size();
127  bestTriangulation=new double[eCount*eCount];
128  midPoint=new int[eCount*eCount];
129  for(int i=0;i<eCount*eCount;i++)
130  bestTriangulation[i]=-1;
131  memset(midPoint,-1,sizeof(int)*eCount*eCount);
132  return GetArea(0,1,vertices);
133 }
134 template<class Real>
135 void MinimalAreaTriangulation<Real>::GetTriangulation(const size_t& i,const size_t& j,const std::vector<Point3D<Real> >& vertices,std::vector<TriangleIndex>& triangles)
136 {
137  TriangleIndex tIndex;
138  size_t eCount=vertices.size();
139  size_t ii=i;
140  if(i<j)
141  ii+=eCount;
142  if(j+1>=ii)
143  return;
144  ii=midPoint[i*eCount+j];
145  //if(ii>=0)
146  //{
147  tIndex.idx[0] = int( i );
148  tIndex.idx[1] = int( j );
149  tIndex.idx[2] = int( ii );
150  triangles.push_back(tIndex);
151  GetTriangulation(i,ii,vertices,triangles);
152  GetTriangulation(ii,j,vertices,triangles);
153  //}
154 }
155 
156 template<class Real>
157 Real MinimalAreaTriangulation<Real>::GetArea(const size_t& i,const size_t& j,const std::vector<Point3D<Real> >& vertices)
158 {
159  Real a=FLT_MAX,temp;
160  size_t eCount=vertices.size();
161  size_t idx=i*eCount+j;
162  size_t ii=i;
163  if(i<j)
164  ii+=eCount;
165  if(j+1>=ii)
166  {
167  bestTriangulation[idx]=0;
168  return 0;
169  }
170  if(midPoint[idx]!=-1)
171  return bestTriangulation[idx];
172  int mid=-1;
173  for(size_t r=j+1;r<ii;r++)
174  {
175  size_t rr=r%eCount;
176  size_t idx1=i*eCount+rr,idx2=rr*eCount+j;
177  Point3D<Real> p,p1,p2;
178  p1=vertices[i]-vertices[rr];
179  p2=vertices[j]-vertices[rr];
180  CrossProduct(p1,p2,p);
181  temp = Real( Length(p) );
182  if(bestTriangulation[idx1]>=0)
183  {
184  temp+=bestTriangulation[idx1];
185  if(temp>a)
186  continue;
187  if(bestTriangulation[idx2]>0)
188  temp+=bestTriangulation[idx2];
189  else
190  temp+=GetArea(rr,j,vertices);
191  }
192  else
193  {
194  if(bestTriangulation[idx2]>=0)
195  temp+=bestTriangulation[idx2];
196  else
197  temp+=GetArea(rr,j,vertices);
198  if(temp>a)
199  continue;
200  temp+=GetArea(i,rr,vertices);
201  }
202 
203  if(temp<a)
204  {
205  a=temp;
206  mid=int(rr);
207  }
208  }
209  bestTriangulation[idx]=a;
210  midPoint[idx]=mid;
211 
212  return a;
213 }