BRL-CAD
rhc_brep.cpp
Go to the documentation of this file.
1 /* R H C _ B R E P . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-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 /** @file rhc_brep.cpp
21  *
22  * Convert a Right Hyperbolic Cylinder to b-rep form
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "raytrace.h"
29 #include "rtgeom.h"
30 #include "brep.h"
31 
32 
33 extern "C" void
34 rt_rhc_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *UNUSED(tol))
35 {
36  struct rt_rhc_internal *eip;
37 
39  eip = (struct rt_rhc_internal *)ip->idb_ptr;
40  RT_RHC_CK_MAGIC(eip);
41 
42  point_t p1_origin;
43  ON_3dPoint plane1_origin, plane2_origin;
44  ON_3dVector plane_x_dir, plane_y_dir;
45 
46  // First, find plane in 3 space corresponding to the bottom face
47  // of the RHC.
48 
49  vect_t x_dir, y_dir;
50 
51  VCROSS(x_dir, eip->rhc_H, eip->rhc_B);
52  VUNITIZE(x_dir);
53  VSCALE(x_dir, x_dir, eip->rhc_r);
54  VMOVE(y_dir, eip->rhc_B);
55 
56  VMOVE(p1_origin, eip->rhc_V);
57  plane1_origin = ON_3dPoint(p1_origin);
58  plane_x_dir = ON_3dVector(x_dir);
59  plane_y_dir = ON_3dVector(y_dir);
60  const ON_Plane rhc_bottom_plane = ON_Plane(plane1_origin, plane_x_dir, plane_y_dir);
61 
62  // Next, create a hyperbolic curve corresponding to the shape of
63  // the hyperboloid in the plane. See if the following webpage
64  // will help:
65  // http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html
66  point_t x_rev_dir, ep1, ep2, ep3, tmppt;
67  VREVERSE(x_rev_dir, x_dir);
68 
69  VADD2(ep1, p1_origin, x_rev_dir);
70  double intercept_calc = (eip->rhc_c)*(eip->rhc_c)/(MAGNITUDE(eip->rhc_B) + eip->rhc_c);
71  double intercept_dist = MAGNITUDE(eip->rhc_B) + eip->rhc_c - intercept_calc;
72  double intercept_length = intercept_dist - MAGNITUDE(eip->rhc_B);
73 
74  double MX = MAGNITUDE(eip->rhc_B);
75  double MP = MX + intercept_length;
76  double w1 = (MX/MP)/(1-MX/MP);
77 
78  VMOVE(tmppt, eip->rhc_B);
79  VUNITIZE(tmppt);
80  VSCALE(tmppt, tmppt, w1 * intercept_dist);
81  VADD2(ep2, p1_origin, tmppt);
82  VADD2(ep3, p1_origin, x_dir);
83  ON_3dPoint onp1 = ON_3dPoint(ep1);
84  ON_3dPoint onp2 = ON_3dPoint(ep2);
85  ON_3dPoint onp3 = ON_3dPoint(ep3);
86 
87  ON_3dPointArray cpts(3);
88  cpts.Append(onp1);
89  cpts.Append(onp2);
90  cpts.Append(onp3);
91  ON_BezierCurve *bcurve = new ON_BezierCurve(cpts);
92  bcurve->MakeRational();
93  bcurve->SetWeight(1, w1);
94 
95  ON_NurbsCurve* hypnurbscurve = ON_NurbsCurve::New();
96 
97  bcurve->GetNurbForm(*hypnurbscurve);
98  delete bcurve;
99 
100  // Also need a straight line from the beginning to the end to
101  // complete the loop.
102 
103  ON_LineCurve* straightedge = new ON_LineCurve(onp3, onp1);
104 
105  // Generate the bottom cap
106  ON_SimpleArray<ON_Curve*> boundary;
107  boundary.Append(ON_Curve::Cast(hypnurbscurve));
108  boundary.Append(ON_Curve::Cast(straightedge));
109 
110  ON_PlaneSurface* bp = new ON_PlaneSurface();
111  bp->m_plane = rhc_bottom_plane;
112  bp->SetDomain(0, -100.0, 100.0);
113  bp->SetDomain(1, -100.0, 100.0);
114  bp->SetExtents(0, bp->Domain(0));
115  bp->SetExtents(1, bp->Domain(1));
116  (*b)->m_S.Append(bp);
117  const int bsi = (*b)->m_S.Count() - 1;
118  ON_BrepFace& bface = (*b)->NewFace(bsi);
119  (*b)->NewPlanarFaceLoop(bface.m_face_index, ON_BrepLoop::outer, boundary, true);
120  const ON_BrepLoop* bloop = (*b)->m_L.Last();
121  bp->SetDomain(0, bloop->m_pbox.m_min.x, bloop->m_pbox.m_max.x);
122  bp->SetDomain(1, bloop->m_pbox.m_min.y, bloop->m_pbox.m_max.y);
123  bp->SetExtents(0, bp->Domain(0));
124  bp->SetExtents(1, bp->Domain(1));
125  (*b)->SetTrimIsoFlags(bface);
126 
127  // Now the side face and top cap - extrude the bottom face and set
128  // the cap flag to true.
129  vect_t vp2;
130  VADD2(vp2, eip->rhc_V, eip->rhc_H);
131  const ON_Curve* extrudepath = new ON_LineCurve(ON_3dPoint(eip->rhc_V), ON_3dPoint(vp2));
132  ON_Brep& brep = *(*b);
133  ON_BrepExtrudeFace(brep, 0, *extrudepath, true);
134  delete extrudepath;
135 
136 }
137 
138 
139 // Local Variables:
140 // tab-width: 8
141 // mode: C++
142 // c-basic-offset: 4
143 // indent-tabs-mode: t
144 // c-file-style: "stroustrup"
145 // End:
146 // ex: shiftwidth=4 tabstop=8
void rt_rhc_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol)
Definition: rhc_brep.cpp:34
Header file for the BRL-CAD common definitions.
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
unsigned char * bp
Definition: rot.c:56
#define UNUSED(parameter)
Definition: common.h:239
Support for uniform tolerances.
Definition: tol.h:71
void * idb_ptr
Definition: raytrace.h:195