exact_geodesic.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 Zhongshi Jiang <jiangzs@nyu.edu>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "exact_geodesic.h"
  9. //Copyright (C) 2008 Danil Kirsanov, MIT License
  10. //Code from https://code.google.com/archive/p/geodesic/
  11. // Compiled into a single file by Zhongshi Jiang
  12. #include <igl/PI.h>
  13. #include <algorithm>
  14. #include <cassert>
  15. #include <cmath>
  16. #include <cstddef>
  17. #include <ctime>
  18. #include <fstream>
  19. #include <iostream>
  20. #include <set>
  21. #include <vector>
  22. #include <memory>
  23. namespace igl{
  24. namespace geodesic{
  25. //#include "geodesic_constants_and_simple_functions.h"
  26. //double const GEODESIC_INF = std::numeric_limits<double>::max();
  27. double const GEODESIC_INF = 1e100;
  28. //in order to avoid numerical problems with "infinitely small" intervals,
  29. //we drop all the intervals smaller than SMALLEST_INTERVAL_RATIO*edge_length
  30. double const SMALLEST_INTERVAL_RATIO = 1e-6;
  31. //double const SMALL_EPSILON = 1e-10;
  32. inline double cos_from_edges(double const a, //compute the cosine of the angle given the lengths of the edges
  33. double const b,
  34. double const c)
  35. {
  36. assert(a>1e-50);
  37. assert(b>1e-50);
  38. assert(c>1e-50);
  39. double result = (b*b + c*c - a*a)/(2.0*b*c);
  40. result = std::max(result, -1.0);
  41. return std::min(result, 1.0);
  42. }
  43. inline double angle_from_edges(double const a, //compute the cosine of the angle given the lengths of the edges
  44. double const b,
  45. double const c)
  46. {
  47. return acos(cos_from_edges(a,b,c));
  48. }
  49. template<class Points, class Faces>
  50. inline bool read_mesh_from_file(char* filename,
  51. Points& points,
  52. Faces& faces)
  53. {
  54. std::ifstream file(filename);
  55. assert(file.is_open());
  56. if(!file.is_open()) return false;
  57. unsigned num_points;
  58. file >> num_points;
  59. assert(num_points>=3);
  60. unsigned num_faces;
  61. file >> num_faces;
  62. points.resize(num_points*3);
  63. for(typename Points::iterator i=points.begin(); i!=points.end(); ++i)
  64. {
  65. file >> *i;
  66. }
  67. faces.resize(num_faces*3);
  68. for(typename Faces::iterator i=faces.begin(); i!=faces.end(); ++i)
  69. {
  70. file >> *i;
  71. }
  72. file.close();
  73. return true;
  74. }
  75. // #include "geodesic_memory"
  76. template<class T> //quickly allocates multiple elements of a given type; no deallocation
  77. class SimlpeMemoryAllocator
  78. {
  79. public:
  80. typedef T* pointer;
  81. SimlpeMemoryAllocator(unsigned block_size = 0,
  82. unsigned max_number_of_blocks = 0)
  83. {
  84. reset(block_size,
  85. max_number_of_blocks);
  86. };
  87. ~SimlpeMemoryAllocator(){};
  88. void reset(unsigned block_size,
  89. unsigned max_number_of_blocks)
  90. {
  91. m_block_size = block_size;
  92. m_max_number_of_blocks = max_number_of_blocks;
  93. m_current_position = 0;
  94. m_storage.reserve(max_number_of_blocks);
  95. m_storage.resize(1);
  96. m_storage[0].resize(block_size);
  97. };
  98. pointer allocate(unsigned const n) //allocate n units
  99. {
  100. assert(n < m_block_size);
  101. if(m_current_position + n >= m_block_size)
  102. {
  103. m_storage.push_back( std::vector<T>() );
  104. m_storage.back().resize(m_block_size);
  105. m_current_position = 0;
  106. }
  107. pointer result = & m_storage.back()[m_current_position];
  108. m_current_position += n;
  109. return result;
  110. };
  111. private:
  112. std::vector<std::vector<T> > m_storage;
  113. unsigned m_block_size; //size of a single block
  114. unsigned m_max_number_of_blocks; //maximum allowed number of blocks
  115. unsigned m_current_position; //first unused element inside the current block
  116. };
  117. template<class T> //quickly allocates and deallocates single elements of a given type
  118. class MemoryAllocator
  119. {
  120. public:
  121. typedef T* pointer;
  122. MemoryAllocator(unsigned block_size = 1024,
  123. unsigned max_number_of_blocks = 1024)
  124. {
  125. reset(block_size,
  126. max_number_of_blocks);
  127. };
  128. ~MemoryAllocator(){};
  129. void clear()
  130. {
  131. reset(m_block_size,
  132. m_max_number_of_blocks);
  133. }
  134. void reset(unsigned block_size,
  135. unsigned max_number_of_blocks)
  136. {
  137. m_block_size = block_size;
  138. m_max_number_of_blocks = max_number_of_blocks;
  139. assert(m_block_size > 0);
  140. assert(m_max_number_of_blocks > 0);
  141. m_current_position = 0;
  142. m_storage.reserve(max_number_of_blocks);
  143. m_storage.resize(1);
  144. m_storage[0].resize(block_size);
  145. m_deleted.clear();
  146. m_deleted.reserve(2*block_size);
  147. };
  148. pointer allocate() //allocates single unit of memory
  149. {
  150. pointer result;
  151. if(m_deleted.empty())
  152. {
  153. if(m_current_position + 1 >= m_block_size)
  154. {
  155. m_storage.push_back( std::vector<T>() );
  156. m_storage.back().resize(m_block_size);
  157. m_current_position = 0;
  158. }
  159. result = & m_storage.back()[m_current_position];
  160. ++m_current_position;
  161. }
  162. else
  163. {
  164. result = m_deleted.back();
  165. m_deleted.pop_back();
  166. }
  167. return result;
  168. };
  169. void deallocate(pointer p) //allocate n units
  170. {
  171. if(m_deleted.size() < m_deleted.capacity())
  172. {
  173. m_deleted.push_back(p);
  174. }
  175. };
  176. private:
  177. std::vector<std::vector<T> > m_storage;
  178. unsigned m_block_size; //size of a single block
  179. unsigned m_max_number_of_blocks; //maximum allowed number of blocks
  180. unsigned m_current_position; //first unused element inside the current block
  181. std::vector<pointer> m_deleted; //pointers to deleted elemets
  182. };
  183. class OutputBuffer
  184. {
  185. public:
  186. OutputBuffer():
  187. m_num_bytes(0)
  188. {}
  189. void clear()
  190. {
  191. m_num_bytes = 0;
  192. m_buffer = std::shared_ptr<double>();
  193. }
  194. template<class T>
  195. T* allocate(unsigned n)
  196. {
  197. double wanted = n*sizeof(T);
  198. if(wanted > m_num_bytes)
  199. {
  200. unsigned new_size = (unsigned) ceil(wanted / (double)sizeof(double));
  201. m_buffer = std::shared_ptr<double>(new double[new_size]);
  202. m_num_bytes = new_size*sizeof(double);
  203. }
  204. return (T*)m_buffer.get();
  205. }
  206. template <class T>
  207. T* get()
  208. {
  209. return (T*)m_buffer.get();
  210. }
  211. template<class T>
  212. unsigned capacity()
  213. {
  214. return (unsigned)floor((double)m_num_bytes/(double)sizeof(T));
  215. };
  216. private:
  217. std::shared_ptr<double> m_buffer;
  218. unsigned m_num_bytes;
  219. };
  220. class Vertex;
  221. class Edge;
  222. class Face;
  223. class Mesh;
  224. class MeshElementBase;
  225. typedef Vertex* vertex_pointer;
  226. typedef Edge* edge_pointer;
  227. typedef Face* face_pointer;
  228. typedef Mesh* mesh_pointer;
  229. typedef MeshElementBase* base_pointer;
  230. template <class Data> //simple vector that stores info about mesh references
  231. class SimpleVector //for efficiency, it uses an outside memory allocator
  232. {
  233. public:
  234. SimpleVector():
  235. m_size(0),
  236. m_begin(NULL)
  237. {};
  238. typedef Data* iterator;
  239. unsigned size(){return m_size;};
  240. iterator begin(){return m_begin;};
  241. iterator end(){return m_begin + m_size;};
  242. template<class DataPointer>
  243. void set_allocation(DataPointer begin, unsigned size)
  244. {
  245. assert(begin != NULL || size == 0);
  246. m_size = size;
  247. m_begin = (iterator)begin;
  248. }
  249. Data& operator[](unsigned i)
  250. {
  251. assert(i < m_size);
  252. return *(m_begin + i);
  253. }
  254. void clear()
  255. {
  256. m_size = 0;
  257. m_begin = NULL;
  258. }
  259. private:
  260. unsigned m_size;
  261. Data* m_begin;
  262. };
  263. enum PointType
  264. {
  265. VERTEX,
  266. EDGE,
  267. FACE,
  268. UNDEFINED_POINT
  269. };
  270. class MeshElementBase //prototype of vertices, edges and faces
  271. {
  272. public:
  273. typedef SimpleVector<vertex_pointer> vertex_pointer_vector;
  274. typedef SimpleVector<edge_pointer> edge_pointer_vector;
  275. typedef SimpleVector<face_pointer> face_pointer_vector;
  276. MeshElementBase():
  277. m_id(0),
  278. m_type(UNDEFINED_POINT)
  279. {};
  280. vertex_pointer_vector& adjacent_vertices(){return m_adjacent_vertices;};
  281. edge_pointer_vector& adjacent_edges(){return m_adjacent_edges;};
  282. face_pointer_vector& adjacent_faces(){return m_adjacent_faces;};
  283. unsigned& id(){return m_id;};
  284. PointType type(){return m_type;};
  285. protected:
  286. vertex_pointer_vector m_adjacent_vertices; //list of the adjacent vertices
  287. edge_pointer_vector m_adjacent_edges; //list of the adjacent edges
  288. face_pointer_vector m_adjacent_faces; //list of the adjacent faces
  289. unsigned m_id; //unique id
  290. PointType m_type; //vertex, edge or face
  291. };
  292. class Point3D //point in 3D and corresponding operations
  293. {
  294. public:
  295. Point3D(){};
  296. Point3D(Point3D* p)
  297. {
  298. x() = p->x();
  299. y() = p->y();
  300. z() = p->z();
  301. };
  302. double* xyz(){return m_coordinates;};
  303. double& x(){return *m_coordinates;};
  304. double& y(){return *(m_coordinates+1);};
  305. double& z(){return *(m_coordinates+2);};
  306. void set(double new_x, double new_y, double new_z)
  307. {
  308. x() = new_x;
  309. y() = new_y;
  310. z() = new_z;
  311. }
  312. void set(double* data)
  313. {
  314. x() = *data;
  315. y() = *(data+1);
  316. z() = *(data+2);
  317. }
  318. double distance(double* v)
  319. {
  320. double dx = m_coordinates[0] - v[0];
  321. double dy = m_coordinates[1] - v[1];
  322. double dz = m_coordinates[2] - v[2];
  323. return sqrt(dx*dx + dy*dy + dz*dz);
  324. };
  325. double distance(Point3D* v)
  326. {
  327. return distance(v->xyz());
  328. };
  329. void add(Point3D* v)
  330. {
  331. x() += v->x();
  332. y() += v->y();
  333. z() += v->z();
  334. };
  335. void multiply(double v)
  336. {
  337. x() *= v;
  338. y() *= v;
  339. z() *= v;
  340. };
  341. private:
  342. double m_coordinates[3]; //xyz
  343. };
  344. class Vertex: public MeshElementBase, public Point3D
  345. {
  346. public:
  347. Vertex()
  348. {
  349. m_type = VERTEX;
  350. };
  351. ~Vertex(){};
  352. bool& saddle_or_boundary(){return m_saddle_or_boundary;};
  353. private:
  354. //this flag speeds up exact geodesic algorithm
  355. bool m_saddle_or_boundary; //it is true if total adjacent angle is larger than 2*PI or this vertex belongs to the mesh boundary
  356. };
  357. class Face: public MeshElementBase
  358. {
  359. public:
  360. Face()
  361. {
  362. m_type = FACE;
  363. };
  364. ~Face(){};
  365. edge_pointer opposite_edge(vertex_pointer v);
  366. vertex_pointer opposite_vertex(edge_pointer e);
  367. edge_pointer next_edge(edge_pointer e, vertex_pointer v);
  368. double vertex_angle(vertex_pointer v)
  369. {
  370. for(unsigned i=0; i<3; ++i)
  371. {
  372. if(adjacent_vertices()[i]->id() == v->id())
  373. {
  374. return m_corner_angles[i];
  375. }
  376. }
  377. assert(0);
  378. return 0;
  379. }
  380. double* corner_angles(){return m_corner_angles;};
  381. private:
  382. double m_corner_angles[3]; //triangle angles in radians; angles correspond to vertices in m_adjacent_vertices
  383. };
  384. class Edge: public MeshElementBase
  385. {
  386. public:
  387. Edge()
  388. {
  389. m_type = EDGE;
  390. };
  391. ~Edge(){};
  392. double& length(){return m_length;};
  393. face_pointer opposite_face(face_pointer f)
  394. {
  395. if(adjacent_faces().size() == 1)
  396. {
  397. assert(adjacent_faces()[0]->id() == f->id());
  398. return NULL;
  399. }
  400. assert(adjacent_faces()[0]->id() == f->id() ||
  401. adjacent_faces()[1]->id() == f->id());
  402. return adjacent_faces()[0]->id() == f->id() ?
  403. adjacent_faces()[1] : adjacent_faces()[0];
  404. };
  405. vertex_pointer opposite_vertex(vertex_pointer v)
  406. {
  407. assert(belongs(v));
  408. return adjacent_vertices()[0]->id() == v->id() ?
  409. adjacent_vertices()[1] : adjacent_vertices()[0];
  410. };
  411. bool belongs(vertex_pointer v)
  412. {
  413. return adjacent_vertices()[0]->id() == v->id() ||
  414. adjacent_vertices()[1]->id() == v->id();
  415. }
  416. bool is_boundary(){return adjacent_faces().size() == 1;};
  417. vertex_pointer v0(){return adjacent_vertices()[0];};
  418. vertex_pointer v1(){return adjacent_vertices()[1];};
  419. void local_coordinates(Point3D* point,
  420. double& x,
  421. double& y)
  422. {
  423. double d0 = point->distance(v0());
  424. if(d0 < 1e-50)
  425. {
  426. x = 0.0;
  427. y = 0.0;
  428. return;
  429. }
  430. double d1 = point->distance(v1());
  431. if(d1 < 1e-50)
  432. {
  433. x = m_length;
  434. y = 0.0;
  435. return;
  436. }
  437. x = m_length/2.0 + (d0*d0 - d1*d1)/(2.0*m_length);
  438. y = sqrt(std::max(0.0, d0*d0 - x*x));
  439. return;
  440. }
  441. private:
  442. double m_length; //length of the edge
  443. };
  444. class SurfacePoint:public Point3D //point on the surface of the mesh
  445. {
  446. public:
  447. SurfacePoint():
  448. m_p(NULL)
  449. {};
  450. SurfacePoint(vertex_pointer v): //set the surface point in the vertex
  451. SurfacePoint::Point3D(v),
  452. m_p(v)
  453. {};
  454. SurfacePoint(face_pointer f): //set the surface point in the center of the face
  455. m_p(f)
  456. {
  457. set(0,0,0);
  458. add(f->adjacent_vertices()[0]);
  459. add(f->adjacent_vertices()[1]);
  460. add(f->adjacent_vertices()[2]);
  461. multiply(1./3.);
  462. };
  463. SurfacePoint(edge_pointer e, //set the surface point in the middle of the edge
  464. double a = 0.5):
  465. m_p(e)
  466. {
  467. double b = 1 - a;
  468. vertex_pointer v0 = e->adjacent_vertices()[0];
  469. vertex_pointer v1 = e->adjacent_vertices()[1];
  470. x() = b*v0->x() + a*v1->x();
  471. y() = b*v0->y() + a*v1->y();
  472. z() = b*v0->z() + a*v1->z();
  473. };
  474. SurfacePoint(base_pointer g,
  475. double x,
  476. double y,
  477. double z,
  478. PointType t = UNDEFINED_POINT):
  479. m_p(g)
  480. {
  481. set(x,y,z);
  482. };
  483. void initialize(SurfacePoint const& p)
  484. {
  485. *this = p;
  486. }
  487. ~SurfacePoint(){};
  488. PointType type(){return m_p ? m_p->type() : UNDEFINED_POINT;};
  489. base_pointer& base_element(){return m_p;};
  490. protected:
  491. base_pointer m_p; //could be face, vertex or edge pointer
  492. };
  493. inline edge_pointer Face::opposite_edge(vertex_pointer v)
  494. {
  495. for(unsigned i=0; i<3; ++i)
  496. {
  497. edge_pointer e = adjacent_edges()[i];
  498. if(!e->belongs(v))
  499. {
  500. return e;
  501. }
  502. }
  503. assert(0);
  504. return NULL;
  505. }
  506. inline vertex_pointer Face::opposite_vertex(edge_pointer e)
  507. {
  508. for(unsigned i=0; i<3; ++i)
  509. {
  510. vertex_pointer v = adjacent_vertices()[i];
  511. if(!e->belongs(v))
  512. {
  513. return v;
  514. }
  515. }
  516. assert(0);
  517. return NULL;
  518. }
  519. inline edge_pointer Face::next_edge(edge_pointer e, vertex_pointer v)
  520. {
  521. assert(e->belongs(v));
  522. for(unsigned i=0; i<3; ++i)
  523. {
  524. edge_pointer next = adjacent_edges()[i];
  525. if(e->id() != next->id() && next->belongs(v))
  526. {
  527. return next;
  528. }
  529. }
  530. assert(0);
  531. return NULL;
  532. }
  533. struct HalfEdge //prototype of the edge; used for mesh construction
  534. {
  535. unsigned face_id;
  536. unsigned vertex_0; //adjacent vertices sorted by id value
  537. unsigned vertex_1; //they are sorted, vertex_0 < vertex_1
  538. };
  539. inline bool operator < (const HalfEdge &x, const HalfEdge &y)
  540. {
  541. if(x.vertex_0 == y.vertex_0)
  542. {
  543. return x.vertex_1 < y.vertex_1;
  544. }
  545. else
  546. {
  547. return x.vertex_0 < y.vertex_0;
  548. }
  549. }
  550. inline bool operator != (const HalfEdge &x, const HalfEdge &y)
  551. {
  552. return x.vertex_0 != y.vertex_0 || x.vertex_1 != y.vertex_1;
  553. }
  554. inline bool operator == (const HalfEdge &x, const HalfEdge &y)
  555. {
  556. return x.vertex_0 == y.vertex_0 && x.vertex_1 == y.vertex_1;
  557. }
  558. struct edge_visible_from_source
  559. {
  560. unsigned source;
  561. edge_pointer edge;
  562. };
  563. class Mesh
  564. {
  565. public:
  566. Mesh()
  567. {};
  568. ~Mesh(){};
  569. template<class Points, class Faces>
  570. void initialize_mesh_data(unsigned num_vertices,
  571. Points& p,
  572. unsigned num_faces,
  573. Faces& tri); //build mesh from regular point-triangle representation
  574. template<class Points, class Faces>
  575. void initialize_mesh_data(Points& p, Faces& tri); //build mesh from regular point-triangle representation
  576. std::vector<Vertex>& vertices(){return m_vertices;};
  577. std::vector<Edge>& edges(){return m_edges;};
  578. std::vector<Face>& faces(){return m_faces;};
  579. unsigned closest_vertices(SurfacePoint* p,
  580. std::vector<vertex_pointer>* storage = NULL); //list vertices closest to the point
  581. private:
  582. void build_adjacencies(); //build internal structure of the mesh
  583. bool verify(); //verifies connectivity of the mesh and prints some debug info
  584. typedef void* void_pointer;
  585. void_pointer allocate_pointers(unsigned n)
  586. {
  587. return m_pointer_allocator.allocate(n);
  588. }
  589. std::vector<Vertex> m_vertices;
  590. std::vector<Edge> m_edges;
  591. std::vector<Face> m_faces;
  592. SimlpeMemoryAllocator<void_pointer> m_pointer_allocator; //fast memory allocating for Face/Vertex/Edge cross-references
  593. };
  594. inline unsigned Mesh::closest_vertices(SurfacePoint* p,
  595. std::vector<vertex_pointer>* storage)
  596. {
  597. assert(p->type() != UNDEFINED_POINT);
  598. if(p->type() == VERTEX)
  599. {
  600. if(storage)
  601. {
  602. storage->push_back(static_cast<vertex_pointer>(p->base_element()));
  603. }
  604. return 1;
  605. }
  606. else if(p->type() == FACE)
  607. {
  608. if(storage)
  609. {
  610. vertex_pointer* vp= p->base_element()->adjacent_vertices().begin();
  611. storage->push_back(*vp);
  612. storage->push_back(*(vp+1));
  613. storage->push_back(*(vp+2));
  614. }
  615. return 2;
  616. }
  617. else if(p->type() == EDGE) //for edge include all 4 adjacent vertices
  618. {
  619. edge_pointer edge = static_cast<edge_pointer>(p->base_element());
  620. if(storage)
  621. {
  622. storage->push_back(edge->adjacent_vertices()[0]);
  623. storage->push_back(edge->adjacent_vertices()[1]);
  624. for(unsigned i = 0; i < edge->adjacent_faces().size(); ++i)
  625. {
  626. face_pointer face = edge->adjacent_faces()[i];
  627. storage->push_back(face->opposite_vertex(edge));
  628. }
  629. }
  630. return 2 + edge->adjacent_faces().size();
  631. }
  632. assert(0);
  633. return 0;
  634. }
  635. template<class Points, class Faces>
  636. void Mesh::initialize_mesh_data(Points& p, Faces& tri) //build mesh from regular point-triangle representation
  637. {
  638. assert(p.size() % 3 == 0);
  639. unsigned const num_vertices = p.size() / 3;
  640. assert(tri.size() % 3 == 0);
  641. unsigned const num_faces = tri.size() / 3;
  642. initialize_mesh_data(num_vertices, p, num_faces, tri);
  643. }
  644. template<class Points, class Faces>
  645. void Mesh::initialize_mesh_data(unsigned num_vertices,
  646. Points& p,
  647. unsigned num_faces,
  648. Faces& tri)
  649. {
  650. unsigned const approximate_number_of_internal_pointers = (num_vertices + num_faces)*4;
  651. unsigned const max_number_of_pointer_blocks = 100;
  652. m_pointer_allocator.reset(approximate_number_of_internal_pointers,
  653. max_number_of_pointer_blocks);
  654. m_vertices.resize(num_vertices);
  655. for(unsigned i=0; i<num_vertices; ++i) //copy coordinates to vertices
  656. {
  657. Vertex& v = m_vertices[i];
  658. v.id() = i;
  659. unsigned shift = 3*i;
  660. v.x() = p[shift];
  661. v.y() = p[shift + 1];
  662. v.z() = p[shift + 2];
  663. }
  664. m_faces.resize(num_faces);
  665. for(unsigned i=0; i<num_faces; ++i) //copy adjacent vertices to polygons/faces
  666. {
  667. Face& f = m_faces[i];
  668. f.id() = i;
  669. f.adjacent_vertices().set_allocation(allocate_pointers(3),3); //allocate three units of memory
  670. unsigned shift = 3*i;
  671. for(unsigned j=0; j<3; ++j)
  672. {
  673. unsigned vertex_index = tri[shift + j];
  674. assert(vertex_index < num_vertices);
  675. f.adjacent_vertices()[j] = &m_vertices[vertex_index];
  676. }
  677. }
  678. build_adjacencies(); //build the structure of the mesh
  679. }
  680. inline void Mesh::build_adjacencies()
  681. {
  682. // Vertex->adjacent Faces
  683. std::vector<unsigned> count(m_vertices.size()); //count adjacent vertices
  684. for(unsigned i=0; i<m_faces.size(); ++i)
  685. {
  686. Face& f = m_faces[i];
  687. for(unsigned j=0; j<3; ++j)
  688. {
  689. unsigned vertex_id = f.adjacent_vertices()[j]->id();
  690. assert(vertex_id < m_vertices.size());
  691. count[vertex_id]++;
  692. }
  693. }
  694. for(unsigned i=0; i<m_vertices.size(); ++i) //reserve space
  695. {
  696. Vertex& v = m_vertices[i];
  697. unsigned num_adjacent_faces = count[i];
  698. v.adjacent_faces().set_allocation(allocate_pointers(num_adjacent_faces), //allocate three units of memory
  699. num_adjacent_faces);
  700. }
  701. std::fill(count.begin(), count.end(), 0);
  702. for(unsigned i=0; i<m_faces.size(); ++i)
  703. {
  704. Face& f = m_faces[i];
  705. for(unsigned j=0; j<3; ++j)
  706. {
  707. vertex_pointer v = f.adjacent_vertices()[j];
  708. v->adjacent_faces()[count[v->id()]++] = &f;
  709. }
  710. }
  711. //find all edges
  712. //i.e. find all half-edges, sort and combine them into edges
  713. std::vector<HalfEdge> half_edges(m_faces.size()*3);
  714. unsigned k = 0;
  715. for(unsigned i=0; i<m_faces.size(); ++i)
  716. {
  717. Face& f = m_faces[i];
  718. for(unsigned j=0; j<3; ++j)
  719. {
  720. half_edges[k].face_id = i;
  721. unsigned vertex_id_1 = f.adjacent_vertices()[j]->id();
  722. unsigned vertex_id_2 = f.adjacent_vertices()[(j+1) % 3]->id();
  723. half_edges[k].vertex_0 = std::min(vertex_id_1, vertex_id_2);
  724. half_edges[k].vertex_1 = std::max(vertex_id_1, vertex_id_2);
  725. k++;
  726. }
  727. }
  728. std::sort(half_edges.begin(), half_edges.end());
  729. unsigned number_of_edges = 1;
  730. for(unsigned i=1; i<half_edges.size(); ++i)
  731. {
  732. if(half_edges[i] != half_edges[i-1])
  733. {
  734. ++number_of_edges;
  735. }
  736. else
  737. {
  738. if(i<half_edges.size()-1) //sanity check: there should be at most two equal half-edges
  739. { //if it fails, most likely the input data are messed up
  740. assert(half_edges[i] != half_edges[i+1]);
  741. }
  742. }
  743. }
  744. // Edges->adjacent Vertices and Faces
  745. m_edges.resize(number_of_edges);
  746. unsigned edge_id = 0;
  747. for(unsigned i=0; i<half_edges.size();)
  748. {
  749. Edge& e = m_edges[edge_id];
  750. e.id() = edge_id++;
  751. e.adjacent_vertices().set_allocation(allocate_pointers(2),2); //allocate two units of memory
  752. e.adjacent_vertices()[0] = &m_vertices[half_edges[i].vertex_0];
  753. e.adjacent_vertices()[1] = &m_vertices[half_edges[i].vertex_1];
  754. e.length() = e.adjacent_vertices()[0]->distance(e.adjacent_vertices()[1]);
  755. assert(e.length() > 1e-100); //algorithm works well with non-degenerate meshes only
  756. if(i != half_edges.size()-1 && half_edges[i] == half_edges[i+1]) //double edge
  757. {
  758. e.adjacent_faces().set_allocation(allocate_pointers(2),2);
  759. e.adjacent_faces()[0] = &m_faces[half_edges[i].face_id];
  760. e.adjacent_faces()[1] = &m_faces[half_edges[i+1].face_id];
  761. i += 2;
  762. }
  763. else //single edge
  764. {
  765. e.adjacent_faces().set_allocation(allocate_pointers(1),1); //one adjucent faces
  766. e.adjacent_faces()[0] = &m_faces[half_edges[i].face_id];
  767. i += 1;
  768. }
  769. }
  770. // Vertices->adjacent Edges
  771. std::fill(count.begin(), count.end(), 0);
  772. for(unsigned i=0; i<m_edges.size(); ++i)
  773. {
  774. Edge& e = m_edges[i];
  775. assert(e.adjacent_vertices().size()==2);
  776. count[e.adjacent_vertices()[0]->id()]++;
  777. count[e.adjacent_vertices()[1]->id()]++;
  778. }
  779. for(unsigned i=0; i<m_vertices.size(); ++i)
  780. {
  781. m_vertices[i].adjacent_edges().set_allocation(allocate_pointers(count[i]),
  782. count[i]);
  783. }
  784. std::fill(count.begin(), count.end(), 0);
  785. for(unsigned i=0; i<m_edges.size(); ++i)
  786. {
  787. Edge& e = m_edges[i];
  788. for(unsigned j=0; j<2; ++j)
  789. {
  790. vertex_pointer v = e.adjacent_vertices()[j];
  791. v->adjacent_edges()[count[v->id()]++] = &e;
  792. }
  793. }
  794. // Faces->adjacent Edges
  795. for(unsigned i=0; i<m_faces.size(); ++i)
  796. {
  797. m_faces[i].adjacent_edges().set_allocation(allocate_pointers(3),3);
  798. }
  799. count.resize(m_faces.size());
  800. std::fill(count.begin(), count.end(), 0);
  801. for(unsigned i=0; i<m_edges.size(); ++i)
  802. {
  803. Edge& e = m_edges[i];
  804. for(unsigned j=0; j<e.adjacent_faces().size(); ++j)
  805. {
  806. face_pointer f = e.adjacent_faces()[j];
  807. assert(count[f->id()]<3);
  808. f->adjacent_edges()[count[f->id()]++] = &e;
  809. }
  810. }
  811. //compute angles for the faces
  812. for(unsigned i=0; i<m_faces.size(); ++i)
  813. {
  814. Face& f = m_faces[i];
  815. double abc[3];
  816. double sum = 0;
  817. for(unsigned j=0; j<3; ++j) //compute angle adjacent to the vertex j
  818. {
  819. for(unsigned k=0; k<3; ++k)
  820. {
  821. vertex_pointer v = f.adjacent_vertices()[(j + k)%3];
  822. abc[k] = f.opposite_edge(v)->length();
  823. }
  824. double angle = angle_from_edges(abc[0], abc[1], abc[2]);
  825. assert(angle>1e-5); //algorithm works well with non-degenerate meshes only
  826. f.corner_angles()[j] = angle;
  827. sum += angle;
  828. }
  829. assert(std::abs(sum - igl::PI) < 1e-5); //algorithm works well with non-degenerate meshes only
  830. }
  831. //define m_turn_around_flag for vertices
  832. std::vector<double> total_vertex_angle(m_vertices.size());
  833. for(unsigned i=0; i<m_faces.size(); ++i)
  834. {
  835. Face& f = m_faces[i];
  836. for(unsigned j=0; j<3; ++j)
  837. {
  838. vertex_pointer v = f.adjacent_vertices()[j];
  839. total_vertex_angle[v->id()] += f.corner_angles()[j];
  840. }
  841. }
  842. for(unsigned i=0; i<m_vertices.size(); ++i)
  843. {
  844. Vertex& v = m_vertices[i];
  845. v.saddle_or_boundary() = (total_vertex_angle[v.id()] > 2.0*igl::PI - 1e-5);
  846. }
  847. for(unsigned i=0; i<m_edges.size(); ++i)
  848. {
  849. Edge& e = m_edges[i];
  850. if(e.is_boundary())
  851. {
  852. e.adjacent_vertices()[0]->saddle_or_boundary() = true;
  853. e.adjacent_vertices()[1]->saddle_or_boundary() = true;
  854. }
  855. }
  856. assert(verify());
  857. }
  858. inline bool Mesh::verify() //verifies connectivity of the mesh and prints some debug info
  859. {
  860. std::cout << std::endl;
  861. // make sure that all vertices are mentioned at least once.
  862. // though the loose vertex is not a bug, it most likely indicates that something is wrong with the mesh
  863. std::vector<bool> map(m_vertices.size(), false);
  864. for(unsigned i=0; i<m_edges.size(); ++i)
  865. {
  866. edge_pointer e = &m_edges[i];
  867. map[e->adjacent_vertices()[0]->id()] = true;
  868. map[e->adjacent_vertices()[1]->id()] = true;
  869. }
  870. assert(std::find(map.begin(), map.end(), false) == map.end());
  871. //make sure that the mesh is connected trough its edges
  872. //if mesh has more than one connected component, it is most likely a bug
  873. std::vector<face_pointer> stack(1,&m_faces[0]);
  874. stack.reserve(m_faces.size());
  875. map.resize(m_faces.size());
  876. std::fill(map.begin(), map.end(), false);
  877. map[0] = true;
  878. while(!stack.empty())
  879. {
  880. face_pointer f = stack.back();
  881. stack.pop_back();
  882. for(unsigned i=0; i<3; ++i)
  883. {
  884. edge_pointer e = f->adjacent_edges()[i];
  885. face_pointer f_adjacent = e->opposite_face(f);
  886. if(f_adjacent && !map[f_adjacent->id()])
  887. {
  888. map[f_adjacent->id()] = true;
  889. stack.push_back(f_adjacent);
  890. }
  891. }
  892. }
  893. assert(std::find(map.begin(), map.end(), false) == map.end());
  894. //print some mesh statistics that can be useful in debugging
  895. // std::cout << "mesh has " << m_vertices.size()
  896. // << " vertices, " << m_faces.size()
  897. // << " faces, " << m_edges.size()
  898. // << " edges\n";
  899. unsigned total_boundary_edges = 0;
  900. double longest_edge = 0;
  901. double shortest_edge = 1e100;
  902. for(unsigned i=0; i<m_edges.size(); ++i)
  903. {
  904. Edge& e = m_edges[i];
  905. total_boundary_edges += e.is_boundary() ? 1 : 0;
  906. longest_edge = std::max(longest_edge, e.length());
  907. shortest_edge = std::min(shortest_edge, e.length());
  908. }
  909. // std::cout << total_boundary_edges << " edges are boundary edges\n";
  910. // std::cout << "shortest/longest edges are "
  911. // << shortest_edge << "/"
  912. // << longest_edge << " = "
  913. // << shortest_edge/longest_edge
  914. // << std::endl;
  915. double minx = 1e100;
  916. double maxx = -1e100;
  917. double miny = 1e100;
  918. double maxy = -1e100;
  919. double minz = 1e100;
  920. double maxz = -1e100;
  921. for(unsigned i=0; i<m_vertices.size(); ++i)
  922. {
  923. Vertex& v = m_vertices[i];
  924. minx = std::min(minx, v.x());
  925. maxx = std::max(maxx, v.x());
  926. miny = std::min(miny, v.y());
  927. maxy = std::max(maxy, v.y());
  928. minz = std::min(minz, v.z());
  929. maxz = std::max(maxz, v.z());
  930. }
  931. // std::cout << "enclosing XYZ box:"
  932. // <<" X[" << minx << "," << maxx << "]"
  933. // <<" Y[" << miny << "," << maxy << "]"
  934. // <<" Z[" << minz << "," << maxz << "]"
  935. // << std::endl;
  936. double dx = maxx - minx;
  937. double dy = maxy - miny;
  938. double dz = maxz - minz;
  939. // std::cout << "approximate diameter of the mesh is "
  940. // << sqrt(dx*dx + dy*dy + dz*dz)
  941. // << std::endl;
  942. double min_angle = 1e100;
  943. double max_angle = -1e100;
  944. for(unsigned i=0; i<m_faces.size(); ++i)
  945. {
  946. Face& f = m_faces[i];
  947. for(unsigned j=0; j<3; ++j)
  948. {
  949. double angle = f.corner_angles()[j];
  950. min_angle = std::min(min_angle, angle);
  951. max_angle = std::max(max_angle, angle);
  952. }
  953. }
  954. // std::cout << "min/max face angles are "
  955. // << min_angle/igl::PI*180.0 << "/"
  956. // << max_angle/igl::PI*180.0
  957. // << " degrees\n";
  958. // std::cout << std::endl;
  959. return true;
  960. }
  961. inline void fill_surface_point_structure(geodesic::SurfacePoint* point,
  962. double* data,
  963. Mesh* mesh)
  964. {
  965. point->set(data);
  966. unsigned type = (unsigned) data[3];
  967. unsigned id = (unsigned) data[4];
  968. if(type == 0) //vertex
  969. {
  970. point->base_element() = &mesh->vertices()[id];
  971. }
  972. else if(type == 1) //edge
  973. {
  974. point->base_element() = &mesh->edges()[id];
  975. }
  976. else //face
  977. {
  978. point->base_element() = &mesh->faces()[id];
  979. }
  980. }
  981. inline void fill_surface_point_double(geodesic::SurfacePoint* point,
  982. double* data,
  983. long mesh_id)
  984. {
  985. data[0] = point->x();
  986. data[1] = point->y();
  987. data[2] = point->z();
  988. data[4] = point->base_element()->id();
  989. if(point->type() == VERTEX) //vertex
  990. {
  991. data[3] = 0;
  992. }
  993. else if(point->type() == EDGE) //edge
  994. {
  995. data[3] = 1;
  996. }
  997. else //face
  998. {
  999. data[3] = 2;
  1000. }
  1001. }
  1002. class Interval;
  1003. class IntervalList;
  1004. typedef Interval* interval_pointer;
  1005. typedef IntervalList* list_pointer;
  1006. class Interval //interval of the edge
  1007. {
  1008. public:
  1009. Interval(){};
  1010. ~Interval(){};
  1011. enum DirectionType
  1012. {
  1013. FROM_FACE_0,
  1014. FROM_FACE_1,
  1015. FROM_SOURCE,
  1016. UNDEFINED_DIRECTION
  1017. };
  1018. double signal(double x) //geodesic distance function at point x
  1019. {
  1020. assert(x>=0.0 && x <= m_edge->length());
  1021. if(m_d == GEODESIC_INF)
  1022. {
  1023. return GEODESIC_INF;
  1024. }
  1025. else
  1026. {
  1027. double dx = x - m_pseudo_x;
  1028. if(m_pseudo_y == 0.0)
  1029. {
  1030. return m_d + std::abs(dx);
  1031. }
  1032. else
  1033. {
  1034. return m_d + sqrt(dx*dx + m_pseudo_y*m_pseudo_y);
  1035. }
  1036. }
  1037. }
  1038. double max_distance(double end)
  1039. {
  1040. if(m_d == GEODESIC_INF)
  1041. {
  1042. return GEODESIC_INF;
  1043. }
  1044. else
  1045. {
  1046. double a = std::abs(m_start - m_pseudo_x);
  1047. double b = std::abs(end - m_pseudo_x);
  1048. return a > b ? m_d + sqrt(a*a + m_pseudo_y*m_pseudo_y):
  1049. m_d + sqrt(b*b + m_pseudo_y*m_pseudo_y);
  1050. }
  1051. }
  1052. void compute_min_distance(double stop) //compute min, given c,d theta, start, end.
  1053. {
  1054. assert(stop > m_start);
  1055. if(m_d == GEODESIC_INF)
  1056. {
  1057. m_min = GEODESIC_INF;
  1058. }
  1059. else if(m_start > m_pseudo_x)
  1060. {
  1061. m_min = signal(m_start);
  1062. }
  1063. else if(stop < m_pseudo_x)
  1064. {
  1065. m_min = signal(stop);
  1066. }
  1067. else
  1068. {
  1069. assert(m_pseudo_y<=0);
  1070. m_min = m_d - m_pseudo_y;
  1071. }
  1072. }
  1073. //compare two intervals in the queue
  1074. bool operator()(interval_pointer const x, interval_pointer const y) const
  1075. {
  1076. if(x->min() != y->min())
  1077. {
  1078. return x->min() < y->min();
  1079. }
  1080. else if(x->start() != y->start())
  1081. {
  1082. return x->start() < y->start();
  1083. }
  1084. else
  1085. {
  1086. return x->edge()->id() < y->edge()->id();
  1087. }
  1088. }
  1089. double stop() //return the endpoint of the interval
  1090. {
  1091. return m_next ? m_next->start() : m_edge->length();
  1092. }
  1093. double hypotenuse(double a, double b)
  1094. {
  1095. return sqrt(a*a + b*b);
  1096. }
  1097. void find_closest_point(double const x,
  1098. double const y,
  1099. double& offset,
  1100. double& distance); //find the point on the interval that is closest to the point (alpha, s)
  1101. double& start(){return m_start;};
  1102. double& d(){return m_d;};
  1103. double& pseudo_x(){return m_pseudo_x;};
  1104. double& pseudo_y(){return m_pseudo_y;};
  1105. double& min(){return m_min;};
  1106. interval_pointer& next(){return m_next;};
  1107. edge_pointer& edge(){return m_edge;};
  1108. DirectionType& direction(){return m_direction;};
  1109. bool visible_from_source(){return m_direction == FROM_SOURCE;};
  1110. unsigned& source_index(){return m_source_index;};
  1111. void initialize(edge_pointer edge,
  1112. SurfacePoint* point = NULL,
  1113. unsigned source_index = 0);
  1114. protected:
  1115. double m_start; //initial point of the interval on the edge
  1116. double m_d; //distance from the source to the pseudo-source
  1117. double m_pseudo_x; //coordinates of the pseudo-source in the local coordinate system
  1118. double m_pseudo_y; //y-coordinate should be always negative
  1119. double m_min; //minimum distance on the interval
  1120. interval_pointer m_next; //pointer to the next interval in the list
  1121. edge_pointer m_edge; //edge that the interval belongs to
  1122. unsigned m_source_index; //the source it belongs to
  1123. DirectionType m_direction; //where the interval is coming from
  1124. };
  1125. struct IntervalWithStop : public Interval
  1126. {
  1127. public:
  1128. double& stop(){return m_stop;};
  1129. protected:
  1130. double m_stop;
  1131. };
  1132. class IntervalList //list of the of intervals of the given edge
  1133. {
  1134. public:
  1135. IntervalList(){m_first = NULL;};
  1136. ~IntervalList(){};
  1137. void clear()
  1138. {
  1139. m_first = NULL;
  1140. };
  1141. void initialize(edge_pointer e)
  1142. {
  1143. m_edge = e;
  1144. m_first = NULL;
  1145. };
  1146. interval_pointer covering_interval(double offset) //returns the interval that covers the offset
  1147. {
  1148. assert(offset >= 0.0 && offset <= m_edge->length());
  1149. interval_pointer p = m_first;
  1150. while(p && p->stop() < offset)
  1151. {
  1152. p = p->next();
  1153. }
  1154. return p;// && p->start() <= offset ? p : NULL;
  1155. };
  1156. void find_closest_point(SurfacePoint* point,
  1157. double& offset,
  1158. double& distance,
  1159. interval_pointer& interval)
  1160. {
  1161. interval_pointer p = m_first;
  1162. distance = GEODESIC_INF;
  1163. interval = NULL;
  1164. double x,y;
  1165. m_edge->local_coordinates(point, x, y);
  1166. while(p)
  1167. {
  1168. if(p->min()<GEODESIC_INF)
  1169. {
  1170. double o, d;
  1171. p->find_closest_point(x, y, o, d);
  1172. if(d < distance)
  1173. {
  1174. distance = d;
  1175. offset = o;
  1176. interval = p;
  1177. }
  1178. }
  1179. p = p->next();
  1180. }
  1181. };
  1182. unsigned number_of_intervals()
  1183. {
  1184. interval_pointer p = m_first;
  1185. unsigned count = 0;
  1186. while(p)
  1187. {
  1188. ++count;
  1189. p = p->next();
  1190. }
  1191. return count;
  1192. }
  1193. interval_pointer last()
  1194. {
  1195. interval_pointer p = m_first;
  1196. if(p)
  1197. {
  1198. while(p->next())
  1199. {
  1200. p = p->next();
  1201. }
  1202. }
  1203. return p;
  1204. }
  1205. double signal(double x)
  1206. {
  1207. interval_pointer interval = covering_interval(x);
  1208. return interval ? interval->signal(x) : GEODESIC_INF;
  1209. }
  1210. interval_pointer& first(){return m_first;};
  1211. edge_pointer& edge(){return m_edge;};
  1212. private:
  1213. interval_pointer m_first; //pointer to the first member of the list
  1214. edge_pointer m_edge; //edge that owns this list
  1215. };
  1216. class SurfacePointWithIndex : public SurfacePoint
  1217. {
  1218. public:
  1219. unsigned index(){return m_index;};
  1220. void initialize(SurfacePoint& p, unsigned index)
  1221. {
  1222. SurfacePoint::initialize(p);
  1223. m_index = index;
  1224. }
  1225. bool operator()(SurfacePointWithIndex* x, SurfacePointWithIndex* y) const //used for sorting
  1226. {
  1227. assert(x->type() != UNDEFINED_POINT && y->type() !=UNDEFINED_POINT);
  1228. if(x->type() != y->type())
  1229. {
  1230. return x->type() < y->type();
  1231. }
  1232. else
  1233. {
  1234. return x->base_element()->id() < y->base_element()->id();
  1235. }
  1236. }
  1237. private:
  1238. unsigned m_index;
  1239. };
  1240. class SortedSources : public std::vector<SurfacePointWithIndex>
  1241. {
  1242. private:
  1243. typedef std::vector<SurfacePointWithIndex*> sorted_vector_type;
  1244. public:
  1245. typedef sorted_vector_type::iterator sorted_iterator;
  1246. typedef std::pair<sorted_iterator, sorted_iterator> sorted_iterator_pair;
  1247. sorted_iterator_pair sources(base_pointer mesh_element)
  1248. {
  1249. m_search_dummy.base_element() = mesh_element;
  1250. return equal_range(m_sorted.begin(),
  1251. m_sorted.end(),
  1252. &m_search_dummy,
  1253. m_compare_less);
  1254. }
  1255. void initialize(std::vector<SurfacePoint>& sources) //we initialize the sources by copie
  1256. {
  1257. resize(sources.size());
  1258. m_sorted.resize(sources.size());
  1259. for(unsigned i=0; i<sources.size(); ++i)
  1260. {
  1261. SurfacePointWithIndex& p = *(begin() + i);
  1262. p.initialize(sources[i],i);
  1263. m_sorted[i] = &p;
  1264. }
  1265. std::sort(m_sorted.begin(), m_sorted.end(), m_compare_less);
  1266. };
  1267. SurfacePointWithIndex& operator[](unsigned i)
  1268. {
  1269. assert(i < size());
  1270. return *(begin() + i);
  1271. }
  1272. private:
  1273. sorted_vector_type m_sorted;
  1274. SurfacePointWithIndex m_search_dummy; //used as a search template
  1275. SurfacePointWithIndex m_compare_less; //used as a compare functor
  1276. };
  1277. inline void Interval::find_closest_point(double const rs,
  1278. double const hs,
  1279. double& r,
  1280. double& d_out) //find the point on the interval that is closest to the point (alpha, s)
  1281. {
  1282. if(m_d == GEODESIC_INF)
  1283. {
  1284. r = GEODESIC_INF;
  1285. d_out = GEODESIC_INF;
  1286. return;
  1287. }
  1288. double hc = -m_pseudo_y;
  1289. double rc = m_pseudo_x;
  1290. double end = stop();
  1291. double local_epsilon = SMALLEST_INTERVAL_RATIO*m_edge->length();
  1292. if(std::abs(hs+hc) < local_epsilon)
  1293. {
  1294. if(rs<=m_start)
  1295. {
  1296. r = m_start;
  1297. d_out = signal(m_start) + std::abs(rs - m_start);
  1298. }
  1299. else if(rs>=end)
  1300. {
  1301. r = end;
  1302. d_out = signal(end) + fabs(end - rs);
  1303. }
  1304. else
  1305. {
  1306. r = rs;
  1307. d_out = signal(rs);
  1308. }
  1309. }
  1310. else
  1311. {
  1312. double ri = (rs*hc + hs*rc)/(hs+hc);
  1313. if(ri<m_start)
  1314. {
  1315. r = m_start;
  1316. d_out = signal(m_start) + hypotenuse(m_start - rs, hs);
  1317. }
  1318. else if(ri>end)
  1319. {
  1320. r = end;
  1321. d_out = signal(end) + hypotenuse(end - rs, hs);
  1322. }
  1323. else
  1324. {
  1325. r = ri;
  1326. d_out = m_d + hypotenuse(rc - rs, hc + hs);
  1327. }
  1328. }
  1329. }
  1330. inline void Interval::initialize(edge_pointer edge,
  1331. SurfacePoint* source,
  1332. unsigned source_index)
  1333. {
  1334. m_next = NULL;
  1335. //m_geodesic_previous = NULL;
  1336. m_direction = UNDEFINED_DIRECTION;
  1337. m_edge = edge;
  1338. m_source_index = source_index;
  1339. m_start = 0.0;
  1340. //m_stop = edge->length();
  1341. if(!source)
  1342. {
  1343. m_d = GEODESIC_INF;
  1344. m_min = GEODESIC_INF;
  1345. return;
  1346. }
  1347. m_d = 0;
  1348. if(source->base_element()->type() == VERTEX)
  1349. {
  1350. if(source->base_element()->id() == edge->v0()->id())
  1351. {
  1352. m_pseudo_x = 0.0;
  1353. m_pseudo_y = 0.0;
  1354. m_min = 0.0;
  1355. return;
  1356. }
  1357. else if(source->base_element()->id() == edge->v1()->id())
  1358. {
  1359. m_pseudo_x = stop();
  1360. m_pseudo_y = 0.0;
  1361. m_min = 0.0;
  1362. return;
  1363. }
  1364. }
  1365. edge->local_coordinates(source, m_pseudo_x, m_pseudo_y);
  1366. m_pseudo_y = -m_pseudo_y;
  1367. compute_min_distance(stop());
  1368. }
  1369. // #include "geodesic_algorithm_base.h"
  1370. class GeodesicAlgorithmBase
  1371. {
  1372. public:
  1373. enum AlgorithmType
  1374. {
  1375. EXACT,
  1376. DIJKSTRA,
  1377. SUBDIVISION,
  1378. UNDEFINED_ALGORITHM
  1379. };
  1380. GeodesicAlgorithmBase(geodesic::Mesh* mesh):
  1381. m_type(UNDEFINED_ALGORITHM),
  1382. m_max_propagation_distance(1e100),
  1383. m_mesh(mesh)
  1384. {};
  1385. virtual ~GeodesicAlgorithmBase(){};
  1386. virtual void propagate(std::vector<SurfacePoint>& sources,
  1387. double max_propagation_distance = GEODESIC_INF, //propagation algorithm stops after reaching the certain distance from the source
  1388. std::vector<SurfacePoint>* stop_points = NULL) = 0; //or after ensuring that all the stop_points are covered
  1389. virtual void trace_back(SurfacePoint& destination, //trace back piecewise-linear path
  1390. std::vector<SurfacePoint>& path) = 0;
  1391. void geodesic(SurfacePoint& source,
  1392. SurfacePoint& destination,
  1393. std::vector<SurfacePoint>& path); //lazy people can find geodesic path with one function call
  1394. void geodesic(std::vector<SurfacePoint>& sources,
  1395. std::vector<SurfacePoint>& destinations,
  1396. std::vector<std::vector<SurfacePoint> >& paths); //lazy people can find geodesic paths with one function call
  1397. virtual unsigned best_source(SurfacePoint& point, //after propagation step is done, quickly find what source this point belongs to and what is the distance to this source
  1398. double& best_source_distance) = 0;
  1399. virtual void print_statistics() //print info about timing and memory usage in the propagation step of the algorithm
  1400. {
  1401. std::cout << "propagation step took " << m_time_consumed << " seconds " << std::endl;
  1402. };
  1403. AlgorithmType type(){return m_type;};
  1404. virtual std::string name();
  1405. geodesic::Mesh* mesh(){return m_mesh;};
  1406. protected:
  1407. void set_stop_conditions(std::vector<SurfacePoint>* stop_points,
  1408. double stop_distance);
  1409. double stop_distance()
  1410. {
  1411. return m_max_propagation_distance;
  1412. }
  1413. AlgorithmType m_type; // type of the algorithm
  1414. typedef std::pair<vertex_pointer, double> stop_vertex_with_distace_type;
  1415. std::vector<stop_vertex_with_distace_type> m_stop_vertices; // algorithm stops propagation after covering certain vertices
  1416. double m_max_propagation_distance; // or reaching the certain distance
  1417. geodesic::Mesh* m_mesh;
  1418. double m_time_consumed; //how much time does the propagation step takes
  1419. double m_propagation_distance_stopped; //at what distance (if any) the propagation algorithm stopped
  1420. };
  1421. inline double length(std::vector<SurfacePoint>& path)
  1422. {
  1423. double length = 0;
  1424. if(!path.empty())
  1425. {
  1426. for(unsigned i=0; i<path.size()-1; ++i)
  1427. {
  1428. length += path[i].distance(&path[i+1]);
  1429. }
  1430. }
  1431. return length;
  1432. }
  1433. inline void print_info_about_path(std::vector<SurfacePoint>& path)
  1434. {
  1435. std::cout << "number of the points in the path = " << path.size()
  1436. << ", length of the path = " << length(path)
  1437. << std::endl;
  1438. }
  1439. inline std::string GeodesicAlgorithmBase::name()
  1440. {
  1441. switch(m_type)
  1442. {
  1443. case EXACT:
  1444. return "exact";
  1445. case DIJKSTRA:
  1446. return "dijkstra";
  1447. case SUBDIVISION:
  1448. return "subdivision";
  1449. default:
  1450. case UNDEFINED_ALGORITHM:
  1451. return "undefined";
  1452. }
  1453. }
  1454. inline void GeodesicAlgorithmBase::geodesic(SurfacePoint& source,
  1455. SurfacePoint& destination,
  1456. std::vector<SurfacePoint>& path) //lazy people can find geodesic path with one function call
  1457. {
  1458. std::vector<SurfacePoint> sources(1, source);
  1459. std::vector<SurfacePoint> stop_points(1, destination);
  1460. double const max_propagation_distance = GEODESIC_INF;
  1461. propagate(sources,
  1462. max_propagation_distance,
  1463. &stop_points);
  1464. trace_back(destination, path);
  1465. }
  1466. inline void GeodesicAlgorithmBase::geodesic(std::vector<SurfacePoint>& sources,
  1467. std::vector<SurfacePoint>& destinations,
  1468. std::vector<std::vector<SurfacePoint> >& paths) //lazy people can find geodesic paths with one function call
  1469. {
  1470. double const max_propagation_distance = GEODESIC_INF;
  1471. propagate(sources,
  1472. max_propagation_distance,
  1473. &destinations); //we use desinations as stop points
  1474. paths.resize(destinations.size());
  1475. for(unsigned i=0; i<paths.size(); ++i)
  1476. {
  1477. trace_back(destinations[i], paths[i]);
  1478. }
  1479. }
  1480. inline void GeodesicAlgorithmBase::set_stop_conditions(std::vector<SurfacePoint>* stop_points,
  1481. double stop_distance)
  1482. {
  1483. m_max_propagation_distance = stop_distance;
  1484. if(!stop_points)
  1485. {
  1486. m_stop_vertices.clear();
  1487. return;
  1488. }
  1489. m_stop_vertices.resize(stop_points->size());
  1490. std::vector<vertex_pointer> possible_vertices;
  1491. for(unsigned i = 0; i < stop_points->size(); ++i)
  1492. {
  1493. SurfacePoint* point = &(*stop_points)[i];
  1494. possible_vertices.clear();
  1495. m_mesh->closest_vertices(point, &possible_vertices);
  1496. vertex_pointer closest_vertex = NULL;
  1497. double min_distance = 1e100;
  1498. for(unsigned j = 0; j < possible_vertices.size(); ++j)
  1499. {
  1500. double distance = point->distance(possible_vertices[j]);
  1501. if(distance < min_distance)
  1502. {
  1503. min_distance = distance;
  1504. closest_vertex = possible_vertices[j];
  1505. }
  1506. }
  1507. assert(closest_vertex);
  1508. m_stop_vertices[i].first = closest_vertex;
  1509. m_stop_vertices[i].second = min_distance;
  1510. }
  1511. }
  1512. class GeodesicAlgorithmExact : public GeodesicAlgorithmBase
  1513. {
  1514. public:
  1515. GeodesicAlgorithmExact(geodesic::Mesh* mesh):
  1516. GeodesicAlgorithmBase(mesh),
  1517. m_memory_allocator(mesh->edges().size(), mesh->edges().size()),
  1518. m_edge_interval_lists(mesh->edges().size())
  1519. {
  1520. m_type = EXACT;
  1521. for(unsigned i=0; i<m_edge_interval_lists.size(); ++i)
  1522. {
  1523. m_edge_interval_lists[i].initialize(&mesh->edges()[i]);
  1524. }
  1525. };
  1526. ~GeodesicAlgorithmExact(){};
  1527. void propagate(std::vector<SurfacePoint>& sources,
  1528. double max_propagation_distance = GEODESIC_INF, //propagation algorithm stops after reaching the certain distance from the source
  1529. std::vector<SurfacePoint>* stop_points = NULL); //or after ensuring that all the stop_points are covered
  1530. void trace_back(SurfacePoint& destination, //trace back piecewise-linear path
  1531. std::vector<SurfacePoint>& path);
  1532. unsigned best_source(SurfacePoint& point, //quickly find what source this point belongs to and what is the distance to this source
  1533. double& best_source_distance);
  1534. void print_statistics();
  1535. private:
  1536. typedef std::set<interval_pointer, Interval> IntervalQueue;
  1537. void update_list_and_queue(list_pointer list,
  1538. IntervalWithStop* candidates, //up to two candidates
  1539. unsigned num_candidates);
  1540. unsigned compute_propagated_parameters(double pseudo_x,
  1541. double pseudo_y,
  1542. double d, //parameters of the interval
  1543. double start,
  1544. double end, //start/end of the interval
  1545. double alpha, //corner angle
  1546. double L, //length of the new edge
  1547. bool first_interval, //if it is the first interval on the edge
  1548. bool last_interval,
  1549. bool turn_left,
  1550. bool turn_right,
  1551. IntervalWithStop* candidates); //if it is the last interval on the edge
  1552. void construct_propagated_intervals(bool invert,
  1553. edge_pointer edge,
  1554. face_pointer face, //constructs iNew from the rest of the data
  1555. IntervalWithStop* candidates,
  1556. unsigned& num_candidates,
  1557. interval_pointer source_interval);
  1558. double compute_positive_intersection(double start,
  1559. double pseudo_x,
  1560. double pseudo_y,
  1561. double sin_alpha,
  1562. double cos_alpha); //used in construct_propagated_intervals
  1563. unsigned intersect_intervals(interval_pointer zero,
  1564. IntervalWithStop* one); //intersecting two intervals with up to three intervals in the end
  1565. interval_pointer best_first_interval(SurfacePoint& point,
  1566. double& best_total_distance,
  1567. double& best_interval_position,
  1568. unsigned& best_source_index);
  1569. bool check_stop_conditions(unsigned& index);
  1570. void clear()
  1571. {
  1572. m_memory_allocator.clear();
  1573. m_queue.clear();
  1574. for(unsigned i=0; i<m_edge_interval_lists.size(); ++i)
  1575. {
  1576. m_edge_interval_lists[i].clear();
  1577. }
  1578. m_propagation_distance_stopped = GEODESIC_INF;
  1579. };
  1580. list_pointer interval_list(edge_pointer e)
  1581. {
  1582. return &m_edge_interval_lists[e->id()];
  1583. };
  1584. void set_sources(std::vector<SurfacePoint>& sources)
  1585. {
  1586. m_sources.initialize(sources);
  1587. }
  1588. void initialize_propagation_data();
  1589. void list_edges_visible_from_source(MeshElementBase* p,
  1590. std::vector<edge_pointer>& storage); //used in initialization
  1591. long visible_from_source(SurfacePoint& point); //used in backtracing
  1592. void best_point_on_the_edge_set(SurfacePoint& point,
  1593. std::vector<edge_pointer> const& storage,
  1594. interval_pointer& best_interval,
  1595. double& best_total_distance,
  1596. double& best_interval_position);
  1597. void possible_traceback_edges(SurfacePoint& point,
  1598. std::vector<edge_pointer>& storage);
  1599. bool erase_from_queue(interval_pointer p);
  1600. IntervalQueue m_queue; //interval queue
  1601. MemoryAllocator<Interval> m_memory_allocator; //quickly allocate and deallocate intervals
  1602. std::vector<IntervalList> m_edge_interval_lists; //every edge has its interval data
  1603. enum MapType {OLD, NEW}; //used for interval intersection
  1604. MapType map[5];
  1605. double start[6];
  1606. interval_pointer i_new[5];
  1607. unsigned m_queue_max_size; //used for statistics
  1608. unsigned m_iterations; //used for statistics
  1609. SortedSources m_sources;
  1610. };
  1611. inline void GeodesicAlgorithmExact::best_point_on_the_edge_set(SurfacePoint& point,
  1612. std::vector<edge_pointer> const& storage,
  1613. interval_pointer& best_interval,
  1614. double& best_total_distance,
  1615. double& best_interval_position)
  1616. {
  1617. best_total_distance = 1e100;
  1618. for(unsigned i=0; i<storage.size(); ++i)
  1619. {
  1620. edge_pointer e = storage[i];
  1621. list_pointer list = interval_list(e);
  1622. double offset;
  1623. double distance;
  1624. interval_pointer interval;
  1625. list->find_closest_point(&point,
  1626. offset,
  1627. distance,
  1628. interval);
  1629. if(distance < best_total_distance)
  1630. {
  1631. best_interval = interval;
  1632. best_total_distance = distance;
  1633. best_interval_position = offset;
  1634. }
  1635. }
  1636. }
  1637. inline void GeodesicAlgorithmExact::possible_traceback_edges(SurfacePoint& point,
  1638. std::vector<edge_pointer>& storage)
  1639. {
  1640. storage.clear();
  1641. if(point.type() == VERTEX)
  1642. {
  1643. vertex_pointer v = static_cast<vertex_pointer>(point.base_element());
  1644. for(unsigned i=0; i<v->adjacent_faces().size(); ++i)
  1645. {
  1646. face_pointer f = v->adjacent_faces()[i];
  1647. storage.push_back(f->opposite_edge(v));
  1648. }
  1649. }
  1650. else if(point.type() == EDGE)
  1651. {
  1652. edge_pointer e = static_cast<edge_pointer>(point.base_element());
  1653. for(unsigned i=0; i<e->adjacent_faces().size(); ++i)
  1654. {
  1655. face_pointer f = e->adjacent_faces()[i];
  1656. storage.push_back(f->next_edge(e,e->v0()));
  1657. storage.push_back(f->next_edge(e,e->v1()));
  1658. }
  1659. }
  1660. else
  1661. {
  1662. face_pointer f = static_cast<face_pointer>(point.base_element());
  1663. storage.push_back(f->adjacent_edges()[0]);
  1664. storage.push_back(f->adjacent_edges()[1]);
  1665. storage.push_back(f->adjacent_edges()[2]);
  1666. }
  1667. }
  1668. inline long GeodesicAlgorithmExact::visible_from_source(SurfacePoint& point) //negative if not visible
  1669. {
  1670. assert(point.type() != UNDEFINED_POINT);
  1671. if(point.type() == EDGE)
  1672. {
  1673. edge_pointer e = static_cast<edge_pointer>(point.base_element());
  1674. list_pointer list = interval_list(e);
  1675. double position = std::min(point.distance(e->v0()), e->length());
  1676. interval_pointer interval = list->covering_interval(position);
  1677. //assert(interval);
  1678. if(interval && interval->visible_from_source())
  1679. {
  1680. return (long)interval->source_index();
  1681. }
  1682. else
  1683. {
  1684. return -1;
  1685. }
  1686. }
  1687. else if(point.type() == FACE)
  1688. {
  1689. return -1;
  1690. }
  1691. else if(point.type() == VERTEX)
  1692. {
  1693. vertex_pointer v = static_cast<vertex_pointer>(point.base_element());
  1694. for(unsigned i=0; i<v->adjacent_edges().size(); ++i)
  1695. {
  1696. edge_pointer e = v->adjacent_edges()[i];
  1697. list_pointer list = interval_list(e);
  1698. double position = e->v0()->id() == v->id() ? 0.0 : e->length();
  1699. interval_pointer interval = list->covering_interval(position);
  1700. if(interval && interval->visible_from_source())
  1701. {
  1702. return (long)interval->source_index();
  1703. }
  1704. }
  1705. return -1;
  1706. }
  1707. assert(0);
  1708. return 0;
  1709. }
  1710. inline double GeodesicAlgorithmExact::compute_positive_intersection(double start,
  1711. double pseudo_x,
  1712. double pseudo_y,
  1713. double sin_alpha,
  1714. double cos_alpha)
  1715. {
  1716. assert(pseudo_y < 0);
  1717. double denominator = sin_alpha*(pseudo_x - start) - cos_alpha*pseudo_y;
  1718. if(denominator<0.0)
  1719. {
  1720. return -1.0;
  1721. }
  1722. double numerator = -pseudo_y*start;
  1723. if(numerator < 1e-30)
  1724. {
  1725. return 0.0;
  1726. }
  1727. if(denominator < 1e-30)
  1728. {
  1729. return -1.0;
  1730. }
  1731. return numerator/denominator;
  1732. }
  1733. inline void GeodesicAlgorithmExact::list_edges_visible_from_source(MeshElementBase* p,
  1734. std::vector<edge_pointer>& storage)
  1735. {
  1736. assert(p->type() != UNDEFINED_POINT);
  1737. if(p->type() == FACE)
  1738. {
  1739. face_pointer f = static_cast<face_pointer>(p);
  1740. for(unsigned i=0; i<3; ++i)
  1741. {
  1742. storage.push_back(f->adjacent_edges()[i]);
  1743. }
  1744. }
  1745. else if(p->type() == EDGE)
  1746. {
  1747. edge_pointer e = static_cast<edge_pointer>(p);
  1748. storage.push_back(e);
  1749. }
  1750. else //VERTEX
  1751. {
  1752. vertex_pointer v = static_cast<vertex_pointer>(p);
  1753. for(unsigned i=0; i<v->adjacent_edges().size(); ++i)
  1754. {
  1755. storage.push_back(v->adjacent_edges()[i]);
  1756. }
  1757. }
  1758. }
  1759. inline bool GeodesicAlgorithmExact::erase_from_queue(interval_pointer p)
  1760. {
  1761. if(p->min() < GEODESIC_INF/10.0)// && p->min >= queue->begin()->first)
  1762. {
  1763. assert(m_queue.count(p)<=1); //the set is unique
  1764. IntervalQueue::iterator it = m_queue.find(p);
  1765. if(it != m_queue.end())
  1766. {
  1767. m_queue.erase(it);
  1768. return true;
  1769. }
  1770. }
  1771. return false;
  1772. }
  1773. inline unsigned GeodesicAlgorithmExact::intersect_intervals(interval_pointer zero,
  1774. IntervalWithStop* one) //intersecting two intervals with up to three intervals in the end
  1775. {
  1776. assert(zero->edge()->id() == one->edge()->id());
  1777. assert(zero->stop() > one->start() && zero->start() < one->stop());
  1778. assert(one->min() < GEODESIC_INF/10.0);
  1779. double const local_epsilon = SMALLEST_INTERVAL_RATIO*one->edge()->length();
  1780. unsigned N=0;
  1781. if(zero->min() > GEODESIC_INF/10.0)
  1782. {
  1783. start[0] = zero->start();
  1784. if(zero->start() < one->start() - local_epsilon)
  1785. {
  1786. map[0] = OLD;
  1787. start[1] = one->start();
  1788. map[1] = NEW;
  1789. N = 2;
  1790. }
  1791. else
  1792. {
  1793. map[0] = NEW;
  1794. N = 1;
  1795. }
  1796. if(zero->stop() > one->stop() + local_epsilon)
  1797. {
  1798. map[N] = OLD; //"zero" interval
  1799. start[N++] = one->stop();
  1800. }
  1801. start[N+1] = zero->stop();
  1802. return N;
  1803. }
  1804. double const local_small_epsilon = 1e-8*one->edge()->length();
  1805. double D = zero->d() - one->d();
  1806. double x0 = zero->pseudo_x();
  1807. double x1 = one->pseudo_x();
  1808. double R0 = x0*x0 + zero->pseudo_y()*zero->pseudo_y();
  1809. double R1 = x1*x1 + one->pseudo_y()*one->pseudo_y();
  1810. double inter[2]; //points of intersection
  1811. char Ninter=0; //number of the points of the intersection
  1812. if(std::abs(D)<local_epsilon) //if d1 == d0, equation is linear
  1813. {
  1814. double denom = x1 - x0;
  1815. if(std::abs(denom)>local_small_epsilon)
  1816. {
  1817. inter[0] = (R1 - R0)/(2.*denom); //one solution
  1818. Ninter = 1;
  1819. }
  1820. }
  1821. else
  1822. {
  1823. double D2 = D*D;
  1824. double Q = 0.5*(R1-R0-D2);
  1825. double X = x0 - x1;
  1826. double A = X*X - D2;
  1827. double B = Q*X + D2*x0;
  1828. double C = Q*Q - D2*R0;
  1829. if (std::abs(A)<local_small_epsilon) //if A == 0, linear equation
  1830. {
  1831. if(std::abs(B)>local_small_epsilon)
  1832. {
  1833. inter[0] = -C/B; //one solution
  1834. Ninter = 1;
  1835. }
  1836. }
  1837. else
  1838. {
  1839. double det = B*B-A*C;
  1840. if(det>local_small_epsilon*local_small_epsilon) //two roots
  1841. {
  1842. det = sqrt(det);
  1843. if(A>0.0) //make sure that the roots are ordered
  1844. {
  1845. inter[0] = (-B - det)/A;
  1846. inter[1] = (-B + det)/A;
  1847. }
  1848. else
  1849. {
  1850. inter[0] = (-B + det)/A;
  1851. inter[1] = (-B - det)/A;
  1852. }
  1853. if(inter[1] - inter[0] > local_small_epsilon)
  1854. {
  1855. Ninter = 2;
  1856. }
  1857. else
  1858. {
  1859. Ninter = 1;
  1860. }
  1861. }
  1862. else if(det>=0.0) //single root
  1863. {
  1864. inter[0] = -B/A;
  1865. Ninter = 1;
  1866. }
  1867. }
  1868. }
  1869. //---------------------------find possible intervals---------------------------------------
  1870. double left = std::max(zero->start(), one->start()); //define left and right boundaries of the intersection of the intervals
  1871. double right = std::min(zero->stop(), one->stop());
  1872. double good_start[4]; //points of intersection within the (left, right) limits +"left" + "right"
  1873. good_start[0] = left;
  1874. char Ngood_start=1; //number of the points of the intersection
  1875. for(char i=0; i<Ninter; ++i) //for all points of intersection
  1876. {
  1877. double x = inter[i];
  1878. if(x > left + local_epsilon && x < right - local_epsilon)
  1879. {
  1880. good_start[Ngood_start++] = x;
  1881. }
  1882. }
  1883. good_start[Ngood_start++] = right;
  1884. MapType mid_map[3];
  1885. for(char i=0; i<Ngood_start-1; ++i)
  1886. {
  1887. double mid = (good_start[i] + good_start[i+1])*0.5;
  1888. mid_map[i] = zero->signal(mid) <= one->signal(mid) ? OLD : NEW;
  1889. }
  1890. //-----------------------------------output----------------------------------
  1891. N = 0;
  1892. if(zero->start() < left - local_epsilon) //additional "zero" interval
  1893. {
  1894. if(mid_map[0] == OLD) //first interval in the map is already the old one
  1895. {
  1896. good_start[0] = zero->start();
  1897. }
  1898. else
  1899. {
  1900. map[N] = OLD; //"zero" interval
  1901. start[N++] = zero->start();
  1902. }
  1903. }
  1904. for(long i=0;i<Ngood_start-1;++i) //for all intervals
  1905. {
  1906. MapType current_map = mid_map[i];
  1907. if(N==0 || map[N-1] != current_map)
  1908. {
  1909. map[N] = current_map;
  1910. start[N++] = good_start[i];
  1911. }
  1912. }
  1913. if(zero->stop() > one->stop() + local_epsilon)
  1914. {
  1915. if(N==0 || map[N-1] == NEW)
  1916. {
  1917. map[N] = OLD; //"zero" interval
  1918. start[N++] = one->stop();
  1919. }
  1920. }
  1921. start[0] = zero->start(); // just to make sure that epsilons do not damage anything
  1922. //start[N] = zero->stop();
  1923. return N;
  1924. }
  1925. inline void GeodesicAlgorithmExact::initialize_propagation_data()
  1926. {
  1927. clear();
  1928. IntervalWithStop candidate;
  1929. std::vector<edge_pointer> edges_visible_from_source;
  1930. for(unsigned i=0; i<m_sources.size(); ++i) //for all edges adjacent to the starting vertex
  1931. {
  1932. SurfacePoint* source = &m_sources[i];
  1933. edges_visible_from_source.clear();
  1934. list_edges_visible_from_source(source->base_element(),
  1935. edges_visible_from_source);
  1936. for(unsigned j=0; j<edges_visible_from_source.size(); ++j)
  1937. {
  1938. edge_pointer e = edges_visible_from_source[j];
  1939. candidate.initialize(e, source, i);
  1940. candidate.stop() = e->length();
  1941. candidate.compute_min_distance(candidate.stop());
  1942. candidate.direction() = Interval::FROM_SOURCE;
  1943. update_list_and_queue(interval_list(e), &candidate, 1);
  1944. }
  1945. }
  1946. }
  1947. inline void GeodesicAlgorithmExact::propagate(std::vector<SurfacePoint>& sources,
  1948. double max_propagation_distance, //propagation algorithm stops after reaching the certain distance from the source
  1949. std::vector<SurfacePoint>* stop_points)
  1950. {
  1951. set_stop_conditions(stop_points, max_propagation_distance);
  1952. set_sources(sources);
  1953. initialize_propagation_data();
  1954. clock_t start = clock();
  1955. unsigned satisfied_index = 0;
  1956. m_iterations = 0; //for statistics
  1957. m_queue_max_size = 0;
  1958. IntervalWithStop candidates[2];
  1959. while(!m_queue.empty())
  1960. {
  1961. m_queue_max_size = std::max(static_cast<unsigned int>(m_queue.size()), m_queue_max_size);
  1962. unsigned const check_period = 10;
  1963. if(++m_iterations % check_period == 0) //check if we covered all required vertices
  1964. {
  1965. if (check_stop_conditions(satisfied_index))
  1966. {
  1967. break;
  1968. }
  1969. }
  1970. interval_pointer min_interval = *m_queue.begin();
  1971. m_queue.erase(m_queue.begin());
  1972. edge_pointer edge = min_interval->edge();
  1973. list_pointer list = interval_list(edge);
  1974. assert(min_interval->d() < GEODESIC_INF);
  1975. bool const first_interval = min_interval->start() == 0.0;
  1976. //bool const last_interval = min_interval->stop() == edge->length();
  1977. bool const last_interval = min_interval->next() == NULL;
  1978. bool const turn_left = edge->v0()->saddle_or_boundary();
  1979. bool const turn_right = edge->v1()->saddle_or_boundary();
  1980. for(unsigned i=0; i<edge->adjacent_faces().size(); ++i) //two possible faces to propagate
  1981. {
  1982. if(!edge->is_boundary()) //just in case, always propagate boundary edges
  1983. {
  1984. if((i == 0 && min_interval->direction() == Interval::FROM_FACE_0) ||
  1985. (i == 1 && min_interval->direction() == Interval::FROM_FACE_1))
  1986. {
  1987. continue;
  1988. }
  1989. }
  1990. face_pointer face = edge->adjacent_faces()[i]; //if we come from 1, go to 2
  1991. edge_pointer next_edge = face->next_edge(edge,edge->v0());
  1992. unsigned num_propagated = compute_propagated_parameters(min_interval->pseudo_x(),
  1993. min_interval->pseudo_y(),
  1994. min_interval->d(), //parameters of the interval
  1995. min_interval->start(),
  1996. min_interval->stop(), //start/end of the interval
  1997. face->vertex_angle(edge->v0()), //corner angle
  1998. next_edge->length(), //length of the new edge
  1999. first_interval, //if it is the first interval on the edge
  2000. last_interval,
  2001. turn_left,
  2002. turn_right,
  2003. candidates); //if it is the last interval on the edge
  2004. bool propagate_to_right = true;
  2005. if(num_propagated)
  2006. {
  2007. if(candidates[num_propagated-1].stop() != next_edge->length())
  2008. {
  2009. propagate_to_right = false;
  2010. }
  2011. bool const invert = next_edge->v0()->id() != edge->v0()->id(); //if the origins coinside, do not invert intervals
  2012. construct_propagated_intervals(invert, //do not inverse
  2013. next_edge,
  2014. face,
  2015. candidates,
  2016. num_propagated,
  2017. min_interval);
  2018. update_list_and_queue(interval_list(next_edge),
  2019. candidates,
  2020. num_propagated);
  2021. }
  2022. if(propagate_to_right)
  2023. {
  2024. //propogation to the right edge
  2025. double length = edge->length();
  2026. next_edge = face->next_edge(edge,edge->v1());
  2027. num_propagated = compute_propagated_parameters(length - min_interval->pseudo_x(),
  2028. min_interval->pseudo_y(),
  2029. min_interval->d(), //parameters of the interval
  2030. length - min_interval->stop(),
  2031. length - min_interval->start(), //start/end of the interval
  2032. face->vertex_angle(edge->v1()), //corner angle
  2033. next_edge->length(), //length of the new edge
  2034. last_interval, //if it is the first interval on the edge
  2035. first_interval,
  2036. turn_right,
  2037. turn_left,
  2038. candidates); //if it is the last interval on the edge
  2039. if(num_propagated)
  2040. {
  2041. bool const invert = next_edge->v0()->id() != edge->v1()->id(); //if the origins coinside, do not invert intervals
  2042. construct_propagated_intervals(invert, //do not inverse
  2043. next_edge,
  2044. face,
  2045. candidates,
  2046. num_propagated,
  2047. min_interval);
  2048. update_list_and_queue(interval_list(next_edge),
  2049. candidates,
  2050. num_propagated);
  2051. }
  2052. }
  2053. }
  2054. }
  2055. m_propagation_distance_stopped = m_queue.empty() ? GEODESIC_INF : (*m_queue.begin())->min();
  2056. clock_t stop = clock();
  2057. m_time_consumed = (static_cast<double>(stop)-static_cast<double>(start))/CLOCKS_PER_SEC;
  2058. /* for(unsigned i=0; i<m_edge_interval_lists.size(); ++i)
  2059. {
  2060. list_pointer list = &m_edge_interval_lists[i];
  2061. interval_pointer p = list->first();
  2062. assert(p->start() == 0.0);
  2063. while(p->next())
  2064. {
  2065. assert(p->stop() == p->next()->start());
  2066. assert(p->d() < GEODESIC_INF);
  2067. p = p->next();
  2068. }
  2069. }*/
  2070. }
  2071. inline bool GeodesicAlgorithmExact::check_stop_conditions(unsigned& index)
  2072. {
  2073. double queue_distance = (*m_queue.begin())->min();
  2074. if(queue_distance < stop_distance())
  2075. {
  2076. return false;
  2077. }
  2078. while(index < m_stop_vertices.size())
  2079. {
  2080. vertex_pointer v = m_stop_vertices[index].first;
  2081. edge_pointer edge = v->adjacent_edges()[0]; //take any edge
  2082. double distance = edge->v0()->id() == v->id() ?
  2083. interval_list(edge)->signal(0.0) :
  2084. interval_list(edge)->signal(edge->length());
  2085. if(queue_distance < distance + m_stop_vertices[index].second)
  2086. {
  2087. return false;
  2088. }
  2089. ++index;
  2090. }
  2091. return true;
  2092. }
  2093. inline void GeodesicAlgorithmExact::update_list_and_queue(list_pointer list,
  2094. IntervalWithStop* candidates, //up to two candidates
  2095. unsigned num_candidates)
  2096. {
  2097. assert(num_candidates <= 2);
  2098. //assert(list->first() != NULL);
  2099. edge_pointer edge = list->edge();
  2100. double const local_epsilon = SMALLEST_INTERVAL_RATIO * edge->length();
  2101. if(list->first() == NULL)
  2102. {
  2103. interval_pointer* p = &list->first();
  2104. IntervalWithStop* first;
  2105. IntervalWithStop* second;
  2106. if(num_candidates == 1)
  2107. {
  2108. first = candidates;
  2109. second = candidates;
  2110. first->compute_min_distance(first->stop());
  2111. }
  2112. else
  2113. {
  2114. if(candidates->start() <= (candidates+1)->start())
  2115. {
  2116. first = candidates;
  2117. second = candidates+1;
  2118. }
  2119. else
  2120. {
  2121. first = candidates+1;
  2122. second = candidates;
  2123. }
  2124. assert(first->stop() == second->start());
  2125. first->compute_min_distance(first->stop());
  2126. second->compute_min_distance(second->stop());
  2127. }
  2128. if(first->start() > 0.0)
  2129. {
  2130. *p = m_memory_allocator.allocate();
  2131. (*p)->initialize(edge);
  2132. p = &(*p)->next();
  2133. }
  2134. *p = m_memory_allocator.allocate();
  2135. memcpy(*p,first,sizeof(Interval));
  2136. m_queue.insert(*p);
  2137. if(num_candidates == 2)
  2138. {
  2139. p = &(*p)->next();
  2140. *p = m_memory_allocator.allocate();
  2141. memcpy(*p,second,sizeof(Interval));
  2142. m_queue.insert(*p);
  2143. }
  2144. if(second->stop() < edge->length())
  2145. {
  2146. p = &(*p)->next();
  2147. *p = m_memory_allocator.allocate();
  2148. (*p)->initialize(edge);
  2149. (*p)->start() = second->stop();
  2150. }
  2151. else
  2152. {
  2153. (*p)->next() = NULL;
  2154. }
  2155. return;
  2156. }
  2157. bool propagate_flag;
  2158. for(unsigned i=0; i<num_candidates; ++i) //for all new intervals
  2159. {
  2160. IntervalWithStop* q = &candidates[i];
  2161. interval_pointer previous = NULL;
  2162. interval_pointer p = list->first();
  2163. assert(p->start() == 0.0);
  2164. while(p != NULL && p->stop() - local_epsilon < q->start())
  2165. {
  2166. p = p->next();
  2167. }
  2168. while(p != NULL && p->start() < q->stop() - local_epsilon) //go through all old intervals
  2169. {
  2170. unsigned const N = intersect_intervals(p, q); //interset two intervals
  2171. if(N == 1)
  2172. {
  2173. if(map[0]==OLD) //if "p" is always better, we do not need to update anything)
  2174. {
  2175. if(previous) //close previous interval and put in into the queue
  2176. {
  2177. previous->next() = p;
  2178. previous->compute_min_distance(p->start());
  2179. m_queue.insert(previous);
  2180. previous = NULL;
  2181. }
  2182. p = p->next();
  2183. }
  2184. else if(previous) //extend previous interval to cover everything; remove p
  2185. {
  2186. previous->next() = p->next();
  2187. erase_from_queue(p);
  2188. m_memory_allocator.deallocate(p);
  2189. p = previous->next();
  2190. }
  2191. else //p becomes "previous"
  2192. {
  2193. previous = p;
  2194. interval_pointer next = p->next();
  2195. erase_from_queue(p);
  2196. memcpy(previous,q,sizeof(Interval));
  2197. previous->start() = start[0];
  2198. previous->next() = next;
  2199. p = next;
  2200. }
  2201. continue;
  2202. }
  2203. //update_flag = true;
  2204. Interval swap(*p); //used for swapping information
  2205. propagate_flag = erase_from_queue(p);
  2206. for(unsigned j=1; j<N; ++j) //no memory is needed for the first one
  2207. {
  2208. i_new[j] = m_memory_allocator.allocate(); //create new intervals
  2209. }
  2210. if(map[0]==OLD) //finish previous, if any
  2211. {
  2212. if(previous)
  2213. {
  2214. previous->next() = p;
  2215. previous->compute_min_distance(previous->stop());
  2216. m_queue.insert(previous);
  2217. previous = NULL;
  2218. }
  2219. i_new[0] = p;
  2220. p->next() = i_new[1];
  2221. p->start() = start[0];
  2222. }
  2223. else if(previous) //extend previous interval to cover everything; remove p
  2224. {
  2225. i_new[0] = previous;
  2226. previous->next() = i_new[1];
  2227. m_memory_allocator.deallocate(p);
  2228. previous = NULL;
  2229. }
  2230. else //p becomes "previous"
  2231. {
  2232. i_new[0] = p;
  2233. memcpy(p,q,sizeof(Interval));
  2234. p->next() = i_new[1];
  2235. p->start() = start[0];
  2236. }
  2237. assert(!previous);
  2238. for(unsigned j=1; j<N; ++j)
  2239. {
  2240. interval_pointer current_interval = i_new[j];
  2241. if(map[j] == OLD)
  2242. {
  2243. memcpy(current_interval,&swap,sizeof(Interval));
  2244. }
  2245. else
  2246. {
  2247. memcpy(current_interval,q,sizeof(Interval));
  2248. }
  2249. if(j == N-1)
  2250. {
  2251. current_interval->next() = swap.next();
  2252. }
  2253. else
  2254. {
  2255. current_interval->next() = i_new[j+1];
  2256. }
  2257. current_interval->start() = start[j];
  2258. }
  2259. for(unsigned j=0; j<N; ++j) //find "min" and add the intervals to the queue
  2260. {
  2261. if(j==N-1 && map[j]==NEW)
  2262. {
  2263. previous = i_new[j];
  2264. }
  2265. else
  2266. {
  2267. interval_pointer current_interval = i_new[j];
  2268. current_interval->compute_min_distance(current_interval->stop()); //compute minimal distance
  2269. if(map[j]==NEW || (map[j]==OLD && propagate_flag))
  2270. {
  2271. m_queue.insert(current_interval);
  2272. }
  2273. }
  2274. }
  2275. p = swap.next();
  2276. }
  2277. if(previous) //close previous interval and put in into the queue
  2278. {
  2279. previous->compute_min_distance(previous->stop());
  2280. m_queue.insert(previous);
  2281. previous = NULL;
  2282. }
  2283. }
  2284. }
  2285. inline unsigned GeodesicAlgorithmExact::compute_propagated_parameters(double pseudo_x,
  2286. double pseudo_y,
  2287. double d, //parameters of the interval
  2288. double begin,
  2289. double end, //start/end of the interval
  2290. double alpha, //corner angle
  2291. double L, //length of the new edge
  2292. bool first_interval, //if it is the first interval on the edge
  2293. bool last_interval,
  2294. bool turn_left,
  2295. bool turn_right,
  2296. IntervalWithStop* candidates) //if it is the last interval on the edge
  2297. {
  2298. assert(pseudo_y<=0.0);
  2299. assert(d<GEODESIC_INF/10.0);
  2300. assert(begin<=end);
  2301. assert(first_interval ? (begin == 0.0) : true);
  2302. IntervalWithStop* p = candidates;
  2303. if(std::abs(pseudo_y) <= 1e-30) //pseudo-source is on the edge
  2304. {
  2305. if(first_interval && pseudo_x <= 0.0)
  2306. {
  2307. p->start() = 0.0;
  2308. p->stop() = L;
  2309. p->d() = d - pseudo_x;
  2310. p->pseudo_x() = 0.0;
  2311. p->pseudo_y() = 0.0;
  2312. return 1;
  2313. }
  2314. else if(last_interval && pseudo_x >= end)
  2315. {
  2316. p->start() = 0.0;
  2317. p->stop() = L;
  2318. p->d() = d + pseudo_x-end;
  2319. p->pseudo_x() = end*cos(alpha);
  2320. p->pseudo_y() = -end*sin(alpha);
  2321. return 1;
  2322. }
  2323. else if(pseudo_x >= begin && pseudo_x <= end)
  2324. {
  2325. p->start() = 0.0;
  2326. p->stop() = L;
  2327. p->d() = d;
  2328. p->pseudo_x() = pseudo_x*cos(alpha);
  2329. p->pseudo_y() = -pseudo_x*sin(alpha);
  2330. return 1;
  2331. }
  2332. else
  2333. {
  2334. return 0;
  2335. }
  2336. }
  2337. double sin_alpha = sin(alpha);
  2338. double cos_alpha = cos(alpha);
  2339. //important: for the first_interval, this function returns zero only if the new edge is "visible" from the source
  2340. //if the new edge can be covered only after turn_over, the value is negative (-1.0)
  2341. double L1 = compute_positive_intersection(begin,
  2342. pseudo_x,
  2343. pseudo_y,
  2344. sin_alpha,
  2345. cos_alpha);
  2346. if(L1 < 0 || L1 >= L)
  2347. {
  2348. if(first_interval && turn_left)
  2349. {
  2350. p->start() = 0.0;
  2351. p->stop() = L;
  2352. p->d() = d + sqrt(pseudo_x*pseudo_x + pseudo_y*pseudo_y);
  2353. p->pseudo_y() = 0.0;
  2354. p->pseudo_x() = 0.0;
  2355. return 1;
  2356. }
  2357. else
  2358. {
  2359. return 0;
  2360. }
  2361. }
  2362. double L2 = compute_positive_intersection(end,
  2363. pseudo_x,
  2364. pseudo_y,
  2365. sin_alpha,
  2366. cos_alpha);
  2367. if(L2 < 0 || L2 >= L)
  2368. {
  2369. p->start() = L1;
  2370. p->stop() = L;
  2371. p->d() = d;
  2372. p->pseudo_x() = cos_alpha*pseudo_x + sin_alpha*pseudo_y;
  2373. p->pseudo_y() = -sin_alpha*pseudo_x + cos_alpha*pseudo_y;
  2374. return 1;
  2375. }
  2376. p->start() = L1;
  2377. p->stop() = L2;
  2378. p->d() = d;
  2379. p->pseudo_x() = cos_alpha*pseudo_x + sin_alpha*pseudo_y;
  2380. p->pseudo_y() = -sin_alpha*pseudo_x + cos_alpha*pseudo_y;
  2381. assert(p->pseudo_y() <= 0.0);
  2382. if(!(last_interval && turn_right))
  2383. {
  2384. return 1;
  2385. }
  2386. else
  2387. {
  2388. p = candidates + 1;
  2389. p->start() = L2;
  2390. p->stop() = L;
  2391. double dx = pseudo_x - end;
  2392. p->d() = d + sqrt(dx*dx + pseudo_y*pseudo_y);
  2393. p->pseudo_x() = end*cos_alpha;
  2394. p->pseudo_y() = -end*sin_alpha;
  2395. return 2;
  2396. }
  2397. }
  2398. inline void GeodesicAlgorithmExact::construct_propagated_intervals(bool invert,
  2399. edge_pointer edge,
  2400. face_pointer face, //constructs iNew from the rest of the data
  2401. IntervalWithStop* candidates,
  2402. unsigned& num_candidates,
  2403. interval_pointer source_interval) //up to two candidates
  2404. {
  2405. double edge_length = edge->length();
  2406. double local_epsilon = SMALLEST_INTERVAL_RATIO * edge_length;
  2407. //kill very small intervals in order to avoid precision problems
  2408. if(num_candidates == 2)
  2409. {
  2410. double start = std::min(candidates->start(), (candidates+1)->start());
  2411. double stop = std::max(candidates->stop(), (candidates+1)->stop());
  2412. if(candidates->stop()-candidates->start() < local_epsilon) // kill interval 0
  2413. {
  2414. *candidates = *(candidates+1);
  2415. num_candidates = 1;
  2416. candidates->start() = start;
  2417. candidates->stop() = stop;
  2418. }
  2419. else if ((candidates+1)->stop() - (candidates+1)->start() < local_epsilon)
  2420. {
  2421. num_candidates = 1;
  2422. candidates->start() = start;
  2423. candidates->stop() = stop;
  2424. }
  2425. }
  2426. IntervalWithStop* first;
  2427. IntervalWithStop* second;
  2428. if(num_candidates == 1)
  2429. {
  2430. first = candidates;
  2431. second = candidates;
  2432. }
  2433. else
  2434. {
  2435. if(candidates->start() <= (candidates+1)->start())
  2436. {
  2437. first = candidates;
  2438. second = candidates+1;
  2439. }
  2440. else
  2441. {
  2442. first = candidates+1;
  2443. second = candidates;
  2444. }
  2445. assert(first->stop() == second->start());
  2446. }
  2447. if(first->start() < local_epsilon)
  2448. {
  2449. first->start() = 0.0;
  2450. }
  2451. if(edge_length - second->stop() < local_epsilon)
  2452. {
  2453. second->stop() = edge_length;
  2454. }
  2455. //invert intervals if necessary; fill missing data and set pointers correctly
  2456. Interval::DirectionType direction = edge->adjacent_faces()[0]->id() == face->id() ?
  2457. Interval::FROM_FACE_0 :
  2458. Interval::FROM_FACE_1;
  2459. if(!invert) //in this case everything is straighforward, we do not have to invert the intervals
  2460. {
  2461. for(unsigned i=0; i<num_candidates; ++i)
  2462. {
  2463. IntervalWithStop* p = candidates + i;
  2464. p->next() = (i == num_candidates - 1) ? NULL : candidates + i + 1;
  2465. p->edge() = edge;
  2466. p->direction() = direction;
  2467. p->source_index() = source_interval->source_index();
  2468. p->min() = 0.0; //it will be changed later on
  2469. assert(p->start() < p->stop());
  2470. }
  2471. }
  2472. else //now we have to invert the intervals
  2473. {
  2474. for(unsigned i=0; i<num_candidates; ++i)
  2475. {
  2476. IntervalWithStop* p = candidates + i;
  2477. p->next() = (i == 0) ? NULL : candidates + i - 1;
  2478. p->edge() = edge;
  2479. p->direction() = direction;
  2480. p->source_index() = source_interval->source_index();
  2481. double length = edge_length;
  2482. p->pseudo_x() = length - p->pseudo_x();
  2483. double start = length - p->stop();
  2484. p->stop() = length - p->start();
  2485. p->start() = start;
  2486. p->min() = 0;
  2487. assert(p->start() < p->stop());
  2488. assert(p->start() >= 0.0);
  2489. assert(p->stop() <= edge->length());
  2490. }
  2491. }
  2492. }
  2493. inline unsigned GeodesicAlgorithmExact::best_source(SurfacePoint& point, //quickly find what source this point belongs to and what is the distance to this source
  2494. double& best_source_distance)
  2495. {
  2496. double best_interval_position;
  2497. unsigned best_source_index;
  2498. best_first_interval(point,
  2499. best_source_distance,
  2500. best_interval_position,
  2501. best_source_index);
  2502. return best_source_index;
  2503. }
  2504. inline interval_pointer GeodesicAlgorithmExact::best_first_interval(SurfacePoint& point,
  2505. double& best_total_distance,
  2506. double& best_interval_position,
  2507. unsigned& best_source_index)
  2508. {
  2509. assert(point.type() != UNDEFINED_POINT);
  2510. interval_pointer best_interval = NULL;
  2511. best_total_distance = GEODESIC_INF;
  2512. if(point.type() == EDGE)
  2513. {
  2514. edge_pointer e = static_cast<edge_pointer>(point.base_element());
  2515. list_pointer list = interval_list(e);
  2516. best_interval_position = point.distance(e->v0());
  2517. best_interval = list->covering_interval(best_interval_position);
  2518. if(best_interval)
  2519. {
  2520. //assert(best_interval && best_interval->d() < GEODESIC_INF);
  2521. best_total_distance = best_interval->signal(best_interval_position);
  2522. best_source_index = best_interval->source_index();
  2523. }
  2524. }
  2525. else if(point.type() == FACE)
  2526. {
  2527. face_pointer f = static_cast<face_pointer>(point.base_element());
  2528. for(unsigned i=0; i<3; ++i)
  2529. {
  2530. edge_pointer e = f->adjacent_edges()[i];
  2531. list_pointer list = interval_list(e);
  2532. double offset;
  2533. double distance;
  2534. interval_pointer interval;
  2535. list->find_closest_point(&point,
  2536. offset,
  2537. distance,
  2538. interval);
  2539. if(interval && distance < best_total_distance)
  2540. {
  2541. best_interval = interval;
  2542. best_total_distance = distance;
  2543. best_interval_position = offset;
  2544. best_source_index = interval->source_index();
  2545. }
  2546. }
  2547. //check for all sources that might be located inside this face
  2548. SortedSources::sorted_iterator_pair local_sources = m_sources.sources(f);
  2549. for(SortedSources::sorted_iterator it=local_sources.first; it != local_sources.second; ++it)
  2550. {
  2551. SurfacePointWithIndex* source = *it;
  2552. double distance = point.distance(source);
  2553. if(distance < best_total_distance)
  2554. {
  2555. best_interval = NULL;
  2556. best_total_distance = distance;
  2557. best_interval_position = 0.0;
  2558. best_source_index = source->index();
  2559. }
  2560. }
  2561. }
  2562. else if(point.type() == VERTEX)
  2563. {
  2564. vertex_pointer v = static_cast<vertex_pointer>(point.base_element());
  2565. for(unsigned i=0; i<v->adjacent_edges().size(); ++i)
  2566. {
  2567. edge_pointer e = v->adjacent_edges()[i];
  2568. list_pointer list = interval_list(e);
  2569. double position = e->v0()->id() == v->id() ? 0.0 : e->length();
  2570. interval_pointer interval = list->covering_interval(position);
  2571. if(interval)
  2572. {
  2573. double distance = interval->signal(position);
  2574. if(distance < best_total_distance)
  2575. {
  2576. best_interval = interval;
  2577. best_total_distance = distance;
  2578. best_interval_position = position;
  2579. best_source_index = interval->source_index();
  2580. }
  2581. }
  2582. }
  2583. }
  2584. if(best_total_distance > m_propagation_distance_stopped) //result is unreliable
  2585. {
  2586. best_total_distance = GEODESIC_INF;
  2587. return NULL;
  2588. }
  2589. else
  2590. {
  2591. return best_interval;
  2592. }
  2593. }
  2594. inline void GeodesicAlgorithmExact::trace_back(SurfacePoint& destination, //trace back piecewise-linear path
  2595. std::vector<SurfacePoint>& path)
  2596. {
  2597. path.clear();
  2598. double best_total_distance;
  2599. double best_interval_position;
  2600. unsigned source_index = std::numeric_limits<unsigned>::max();
  2601. interval_pointer best_interval = best_first_interval(destination,
  2602. best_total_distance,
  2603. best_interval_position,
  2604. source_index);
  2605. if(best_total_distance >= GEODESIC_INF/2.0) //unable to find the right path
  2606. {
  2607. return;
  2608. }
  2609. path.push_back(destination);
  2610. if(best_interval) //if we did not hit the face source immediately
  2611. {
  2612. std::vector<edge_pointer> possible_edges;
  2613. possible_edges.reserve(10);
  2614. while(visible_from_source(path.back()) < 0) //while this point is not in the direct visibility of some source (if we are inside the FACE, we obviously hit the source)
  2615. {
  2616. SurfacePoint& q = path.back();
  2617. possible_traceback_edges(q, possible_edges);
  2618. interval_pointer interval;
  2619. double total_distance;
  2620. double position;
  2621. best_point_on_the_edge_set(q,
  2622. possible_edges,
  2623. interval,
  2624. total_distance,
  2625. position);
  2626. //std::cout << total_distance + length(path) << std::endl;
  2627. assert(total_distance<GEODESIC_INF);
  2628. source_index = interval->source_index();
  2629. edge_pointer e = interval->edge();
  2630. double local_epsilon = SMALLEST_INTERVAL_RATIO*e->length();
  2631. if(position < local_epsilon)
  2632. {
  2633. path.push_back(SurfacePoint(e->v0()));
  2634. }
  2635. else if(position > e->length()-local_epsilon)
  2636. {
  2637. path.push_back(SurfacePoint(e->v1()));
  2638. }
  2639. else
  2640. {
  2641. double normalized_position = position/e->length();
  2642. path.push_back(SurfacePoint(e, normalized_position));
  2643. }
  2644. }
  2645. }
  2646. SurfacePoint& source = static_cast<SurfacePoint&>(m_sources[source_index]);
  2647. if(path.back().distance(&source) > 0)
  2648. {
  2649. path.push_back(source);
  2650. }
  2651. }
  2652. inline void GeodesicAlgorithmExact::print_statistics()
  2653. {
  2654. GeodesicAlgorithmBase::print_statistics();
  2655. unsigned interval_counter = 0;
  2656. for(unsigned i=0; i<m_edge_interval_lists.size(); ++i)
  2657. {
  2658. interval_counter += m_edge_interval_lists[i].number_of_intervals();
  2659. }
  2660. double intervals_per_edge = (double)interval_counter/(double)m_edge_interval_lists.size();
  2661. double memory = m_edge_interval_lists.size()*sizeof(IntervalList) +
  2662. interval_counter*sizeof(Interval);
  2663. std::cout << "uses about " << memory/1e6 << "Mb of memory" <<std::endl;
  2664. std::cout << interval_counter << " total intervals, or "
  2665. << intervals_per_edge << " intervals per edge"
  2666. << std::endl;
  2667. std::cout << "maximum interval queue size is " << m_queue_max_size << std::endl;
  2668. std::cout << "number of interval propagations is " << m_iterations << std::endl;
  2669. }
  2670. } //geodesic
  2671. }
  2672. template <
  2673. typename DerivedV,
  2674. typename DerivedF,
  2675. typename DerivedVS,
  2676. typename DerivedFS,
  2677. typename DerivedVT,
  2678. typename DerivedFT,
  2679. typename DerivedD>
  2680. IGL_INLINE void igl::exact_geodesic(
  2681. const Eigen::MatrixBase<DerivedV> &V,
  2682. const Eigen::MatrixBase<DerivedF> &F,
  2683. const Eigen::MatrixBase<DerivedVS> &VS,
  2684. const Eigen::MatrixBase<DerivedFS> &FS,
  2685. const Eigen::MatrixBase<DerivedVT> &VT,
  2686. const Eigen::MatrixBase<DerivedFT> &FT,
  2687. Eigen::PlainObjectBase<DerivedD> &D)
  2688. {
  2689. assert(V.cols() == 3 && F.cols() == 3 && "Only support 3D triangle mesh");
  2690. assert(VS.cols() <=1 && FS.cols() <= 1 && VT.cols() <= 1 && FT.cols() <=1 && "Only support one dimensional inputs");
  2691. std::vector<typename DerivedV::Scalar> points(V.rows() * V.cols());
  2692. std::vector<typename DerivedF::Scalar> faces(F.rows() * F.cols());
  2693. for (int i = 0; i < points.size(); i++)
  2694. {
  2695. points[i] = V(i / 3, i % 3);
  2696. }
  2697. for (int i = 0; i < faces.size(); i++)
  2698. {
  2699. faces[i] = F(i / 3, i % 3);
  2700. }
  2701. igl::geodesic::Mesh mesh;
  2702. mesh.initialize_mesh_data(points, faces);
  2703. igl::geodesic::GeodesicAlgorithmExact exact_algorithm(&mesh);
  2704. std::vector<igl::geodesic::SurfacePoint> source(VS.rows() + FS.rows());
  2705. std::vector<igl::geodesic::SurfacePoint> target(VT.rows() + FT.rows());
  2706. for (int i = 0; i < VS.rows(); i++)
  2707. {
  2708. source[i] = (igl::geodesic::SurfacePoint(&mesh.vertices()[VS(i)]));
  2709. }
  2710. for (int i = 0; i < FS.rows(); i++)
  2711. {
  2712. source[i] = (igl::geodesic::SurfacePoint(&mesh.faces()[FS(i)]));
  2713. }
  2714. for (int i = 0; i < VT.rows(); i++)
  2715. {
  2716. target[i] = (igl::geodesic::SurfacePoint(&mesh.vertices()[VT(i)]));
  2717. }
  2718. for (int i = 0; i < FT.rows(); i++)
  2719. {
  2720. target[i] = (igl::geodesic::SurfacePoint(&mesh.faces()[FT(i)]));
  2721. }
  2722. exact_algorithm.propagate(source);
  2723. std::vector<igl::geodesic::SurfacePoint> path;
  2724. D.resize(target.size(), 1);
  2725. for (int i = 0; i < target.size(); i++)
  2726. {
  2727. exact_algorithm.trace_back(target[i], path);
  2728. D(i) = igl::geodesic::length(path);
  2729. }
  2730. }
  2731. #ifdef IGL_STATIC_LIBRARY
  2732. template void igl::exact_geodesic<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>>(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1>> const &, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1>> const &, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>> const &, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>> const &, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>> const &, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1>> const &, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1>> &);
  2733. template void igl::exact_geodesic<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  2734. #endif