HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_LineTree.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_LineTree.h (C++)
7  *
8  * COMMENTS:
9  *
10  * This implements an PMR-Tree/octree variant designed for sorting
11  * line segments in 3-space for easy access.
12  *
13  */
14 
15 #ifndef __UT_LineTree_h__
16 #define __UT_LineTree_h__
17 
18 #include "UT_API.h"
19 #include "UT_Array.h"
20 #include "UT_BoundingBox.h"
21 #include "UT_Format.h"
22 #include "UT_UniquePtr.h"
23 #include "UT_VectorTypes.h"
24 
25 
26 class utLineTreeNode;
28 class utLeafNode;
29 
31 {
32 public:
33  UT_LineTree();
34  ~UT_LineTree();
35 
36  UT_LineTree(const UT_LineTree &) = delete;
37  UT_LineTree &operator=(const UT_LineTree &) = delete;
38 
39  // add a line segment, returns index of line segment
40  int addLine( const UT_Vector3 &pt1, const UT_Vector3 &pt2 );
41 
42  // return the number of lines in the tree
43  int size() const { return myLineList.size(); }
44 
45  // If you know how many lines you'll add, use this to allocate ahead of
46  // time.
47  void setCapacity( unsigned capacity )
48  {
49  myLineList.setCapacity(capacity);
50  }
51 
52  // build the tree using the line segments added with addLine
53  // returns false if tree is already built or no lines have been added,
54  //
55  bool buildTree();
56 
57  // removes all line segments from tree
58  void clear();
59 
60  // finds the nearest line id to the given point.
61  // if dist2 != NULL, then it is stored the squared distance from the
62  // found line segment and the point pt.
63  // returns -1 if pt is not within our bounding box or the tree is not
64  // build yet
65  int findNearestLineSegment( const UT_Vector3 &pt, float *dist2 = NULL ) const;
66 
67  // finds the k nearest line segments to the given point.
68  // the results are stored into the buffers given by the caller
69  // return value is the number of actual line segments found
70  unsigned findKNearestLineSegments( const UT_Vector3 &pt,
71  int line_id[],
72  float line_dist2[],
73  unsigned max_lines,
74  float max_dist2) const;
75 
76  unsigned findKNearestLineSegments( const UT_Vector3 &pt,
77  int line_id[],
78  float line_dist2[],
79  unsigned max_lines ) const;
80 
81  // set the threshold for splitting up;
82  // will only take effect when starting a new tree!
83  void requestThreshold( unsigned threshold )
84  { myRequestedThreshold = threshold; }
85 
86  // retrieves the added line segment by id
87  // returns true if found, otherwise false
88  bool getLineById( int id, UT_Vector3 &pt1, UT_Vector3 &pt2 );
89 
90  // reverse the positions of the given line segment
91  void reverseLine(int id)
92  { myLineList(id).reverse(); }
93 
94  const UT_Vector3& linePos1(int id) const { return myLineList(id).myP1; }
95  const UT_Vector3& linePos2(int id) const { return myLineList(id).myP2; }
96 
97  // outputs the tree to stdout for debugging
98  void dumpTree(bool verbose = true) const;
99 
100 private:
101  struct Line {
102  Line() {}
103  Line(const UT_Vector3 &p1, const UT_Vector3 &p2)
104  : myP1(p1), myP2(p2)
105  {
106  myIDir = 1.0F / (myP2 - myP1);
107  }
108  void reverse()
109  {
110  UT_Vector3 p = myP1;
111  myP1 = myP2;
112  myP2 = p;
113  myIDir = 1.0F / (myP2 - myP1);
114  }
115 
116  UT_Vector3 myP1;
117  UT_Vector3 myP2;
118  UT_Vector3 myIDir;
119  };
120 
121  class IsInside {
122  public:
123  IsInside(const UT_BoundingBox &box,
124  const UT_Array<Line> &list)
125  : myBox(box)
126  , myLineList(list) {}
127  bool operator()(int idx) const
128  {
129  return myBox.isLineInside(myLineList(idx).myP1,
130  myLineList(idx).myIDir);
131  }
132  private:
133  const UT_BoundingBox &myBox;
134  const UT_Array<Line> &myLineList;
135  };
136 
137  utLineTreeNodePtr buildTree(const UT_BoundingBox &box,
138  int *lines, int nlines, int depth);
139  void dumpTree(const utLineTreeNode *node,
140  bool verbose,
141  unsigned &num_octnodes,
142  unsigned &num_leafnodes,
143  double &num_leaflines,
144  unsigned &max_depth,
145  unsigned depth = 0) const;
146 
147  void getKClosestLine(
148  const utLeafNode *tree,
149  const UT_Vector3 &pt,
150  int line_idx[],
151  float mindist2[],
152  unsigned &num_lines,
153  unsigned max_lines,
154  float in_max_dist2) const;
155 
156 private:
157  UT_Array<Line> myLineList;
159  unsigned myRequestedThreshold;
160  unsigned myThreshold;
161 };
162 
163 #endif // __UT_LineTree_h__
void setCapacity(unsigned capacity)
Definition: UT_LineTree.h:47
int size() const
Definition: UT_LineTree.h:43
void reverse(I begin, I end)
Definition: pugixml.cpp:7190
#define UT_API
Definition: UT_API.h:14
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
void requestThreshold(unsigned threshold)
Definition: UT_LineTree.h:83
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
UT_UniquePtr< utLineTreeNode > utLineTreeNodePtr
Definition: UT_LineTree.h:27
const UT_Vector3 & linePos1(int id) const
Definition: UT_LineTree.h:94
Type-safe formatting, modeled on the Python str.format function.
const UT_Vector3 & linePos2(int id) const
Definition: UT_LineTree.h:95
myRoot
Definition: UT_RTreeImpl.h:716
void reverseLine(int id)
Definition: UT_LineTree.h:91