BRL-CAD
Subcurve.cpp
Go to the documentation of this file.
1 /* S U B C U R V E . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 #include "brep.h"
23 
24 Subcurve::Subcurve()
25  : m_curve(NULL), m_islinear(false)
26 {
27  m_children[0] = m_children[1] = NULL;
28 }
29 
30 Subcurve::Subcurve(ON_Curve *curve)
31 {
32  m_curve = curve;
33  if (curve) {
34  m_node = curve->BoundingBox();
35  m_t = curve->Domain();
36  m_islinear = curve->IsLinear();
37  } else {
38  m_islinear = false;
39  }
40  m_children[0] = m_children[1] = NULL;
41 }
42 
43 Subcurve::Subcurve(const Subcurve &_scurve)
44 {
45  m_islinear = _scurve.m_islinear;
46  m_curve = _scurve.m_curve->Duplicate();
47  m_t = _scurve.m_t;
48  m_children[0] = m_children[1] = NULL;
49  SetBBox(_scurve.m_node);
50 }
51 
52 Subcurve::~Subcurve()
53 {
54  for (int i = 0; i < 2; i++) {
55  if (m_children[i]) {
56  delete m_children[i];
57  }
58  }
59  delete m_curve;
60 }
61 
62 int
63 Subcurve::Split()
64 {
65  if (m_children[0] && m_children[1]) {
66  return 0;
67  }
68 
69  for (int i = 0; i < 2; i++) {
70  m_children[i] = new Subcurve();
71  }
72  ON_SimpleArray<double> pline_t;
73  double split_t = m_t.Mid();
74  if (m_curve->IsPolyline(NULL, &pline_t) && pline_t.Count() > 2) {
75  split_t = pline_t[pline_t.Count() / 2];
76  }
77  if (m_curve->Split(split_t, m_children[0]->m_curve, m_children[1]->m_curve) == false) {
78  return -1;
79  }
80 
81  m_children[0]->m_t = m_children[0]->m_curve->Domain();
82  m_children[0]->SetBBox(m_children[0]->m_curve->BoundingBox());
83  m_children[0]->m_islinear = m_children[0]->m_curve->IsLinear();
84  m_children[1]->m_t = m_children[1]->m_curve->Domain();
85  m_children[1]->SetBBox(m_children[1]->m_curve->BoundingBox());
86  m_children[1]->m_islinear = m_children[1]->m_curve->IsLinear();
87 
88  return 0;
89 }
90 
91 void
92 Subcurve::GetBBox(ON_3dPoint &min, ON_3dPoint &max)
93 {
94  min = m_node.m_min;
95  max = m_node.m_max;
96 }
97 
98 void
99 Subcurve::SetBBox(const ON_BoundingBox &bbox)
100 {
101  m_node = bbox;
102 }
103 
104 bool
105 Subcurve::IsPointIn(const ON_3dPoint &pt, double tolerance /* = 0.0 */)
106 {
107  ON_3dVector vtol(tolerance, tolerance, tolerance);
108  ON_BoundingBox new_bbox(m_node.m_min - vtol, m_node.m_max + vtol);
109  return new_bbox.IsPointIn(pt);
110 }
111 
112 bool
113 Subcurve::Intersect(const Subcurve &other,
114  double tolerance /* = 0.0 */,
115  ON_BoundingBox *intersection /* = NULL */) const
116 {
117  ON_3dVector vtol(tolerance, tolerance, tolerance);
118  ON_BoundingBox new_bbox(m_node.m_min - vtol, m_node.m_max + vtol);
119  ON_BoundingBox box;
120  bool ret = box.Intersection(new_bbox, other.m_node);
121  if (intersection != NULL) {
122  *intersection = box;
123  }
124  return ret;
125 }
126 
127 // Local Variables:
128 // tab-width: 8
129 // mode: C++
130 // c-basic-offset: 4
131 // indent-tabs-mode: t
132 // c-file-style: "stroustrup"
133 // End:
134 // ex: shiftwidth=4 tabstop=8
Header file for the BRL-CAD common definitions.
Definition: human.c:197