BRL-CAD
tgc_brep.cpp
Go to the documentation of this file.
1 /* T G 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 tgc_brep.cpp
21  *
22  * Convert a Truncated General Cone 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_tgc_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *UNUSED(tol))
35 {
36  struct rt_tgc_internal *eip;
37 
39  eip = (struct rt_tgc_internal *)ip->idb_ptr;
40  RT_TGC_CK_MAGIC(eip);
41 
42  point_t p1_origin, p2_origin;
43  ON_3dPoint plane1_origin, plane2_origin;
44  ON_3dVector plane1_x_dir, plane1_y_dir, plane2_x_dir, plane2_y_dir;
45 
46  double ell1_axis_len_1, ell1_axis_len_2, ell2_axis_len_1, ell2_axis_len_2;
47 
48  // First, find planes in 3 space with x and y axes along the axis
49  // of the ellipses defining the top and bottom of the TGC, with
50  // coordinate origins at the center of the ellipses. This
51  // information will be needed for the ruled surface definition
52  // (for the sides) and also for the trimmed planes needed for the
53  // top and bottom of the volume.
54 
55  vect_t tmp, x1_dir, y1_dir, x2_dir, y2_dir;
56 
57  VMOVE(x1_dir, eip->a);
58  VMOVE(y1_dir, eip->b);
59 
60  VMOVE(x2_dir, eip->c);
61  VMOVE(y2_dir, eip->d);
62 
63  VMOVE(p1_origin, eip->v);
64  VMOVE(tmp, eip->v);
65  VADD2(p2_origin, tmp, eip->h);
66  plane1_origin = ON_3dPoint(p1_origin);
67  plane1_x_dir = ON_3dVector(x1_dir);
68  plane1_y_dir = ON_3dVector(y1_dir);
69  plane2_origin = ON_3dPoint(p2_origin);
70  plane2_x_dir = ON_3dVector(x2_dir);
71  plane2_y_dir = ON_3dVector(y2_dir);
72 
73  const ON_Plane ell1_plane = ON_Plane(plane1_origin, plane1_x_dir, plane1_y_dir);
74  const ON_Plane ell2_plane = ON_Plane(plane2_origin, plane2_x_dir, plane2_y_dir);
75 
76  // Once the planes have been created, create the ellipses within
77  // the planes.
78  ell1_axis_len_1 = MAGNITUDE(eip->a);
79  ell1_axis_len_2 = MAGNITUDE(eip->b);
80  ell2_axis_len_1 = MAGNITUDE(eip->c);
81  ell2_axis_len_2 = MAGNITUDE(eip->d);
82 
83  ON_Ellipse ellipse1 = ON_Ellipse(ell1_plane, ell1_axis_len_1, ell1_axis_len_2);
84  ON_Ellipse ellipse2 = ON_Ellipse(ell2_plane, ell2_axis_len_1, ell2_axis_len_2);
85 
86  // Generate an ON_Curves from the ellipses
87  ON_NurbsCurve* ellcurve1 = ON_NurbsCurve::New();
88  ellipse1.GetNurbForm((*ellcurve1));
89  ON_NurbsCurve* ellcurve2 = ON_NurbsCurve::New();
90  ellipse2.GetNurbForm((*ellcurve2));
91  ellcurve1->SetDomain(0.0, 1.0);
92  ellcurve2->SetDomain(0.0, 1.0);
93 
94  // Create the side surface with
95  // ON_NurbsSurface::CreateRuledSurface and the top and bottom
96  // planes by using the ellipses as outer trimming curves - define
97  // UV surfaces for the top and bottom such that they contain the
98  // ellipses.
99  ON_SimpleArray<ON_Curve*> bottomboundary;
100  bottomboundary.Append(ON_Curve::Cast(ellcurve1));
101  ON_PlaneSurface* bp = new ON_PlaneSurface();
102  bp->m_plane = ell1_plane;
103  bp->SetDomain(0, -100.0, 100.0);
104  bp->SetDomain(1, -100.0, 100.0);
105  bp->SetExtents(0, bp->Domain(0));
106  bp->SetExtents(1, bp->Domain(1));
107  const int bsi = (*b)->AddSurface(bp);
108  ON_BrepFace& bface = (*b)->NewFace(bsi);
109  (*b)->NewPlanarFaceLoop(bface.m_face_index, ON_BrepLoop::outer, bottomboundary, true);
110  const ON_BrepLoop* bloop = (*b)->m_L.Last();
111  bp->SetDomain(0, bloop->m_pbox.m_min.x, bloop->m_pbox.m_max.x);
112  bp->SetDomain(1, bloop->m_pbox.m_min.y, bloop->m_pbox.m_max.y);
113  bp->SetExtents(0, bp->Domain(0));
114  bp->SetExtents(1, bp->Domain(1));
115  (*b)->SetTrimIsoFlags(bface);
116  (*b)->FlipFace(bface);
117 
118  ON_SimpleArray<ON_Curve*> topboundary;
119  topboundary.Append(ON_Curve::Cast(ellcurve2));
120  ON_PlaneSurface* tp = new ON_PlaneSurface();
121  tp->m_plane = ell2_plane;
122  tp->SetDomain(0, -100.0, 100.0);
123  tp->SetDomain(1, -100.0, 100.0);
124  tp->SetExtents(0, tp->Domain(0));
125  tp->SetExtents(1, tp->Domain(1));
126  const int tsi = (*b)->AddSurface(tp);
127  ON_BrepFace& tface = (*b)->NewFace(tsi);
128  (*b)->NewPlanarFaceLoop(tface.m_face_index, ON_BrepLoop::outer, topboundary, true);
129  const ON_BrepLoop* tloop = (*b)->m_L.Last();
130  tp->SetDomain(0, tloop->m_pbox.m_min.x, tloop->m_pbox.m_max.x);
131  tp->SetDomain(1, tloop->m_pbox.m_min.y, tloop->m_pbox.m_max.y);
132  tp->SetExtents(0, tp->Domain(0));
133  tp->SetExtents(1, tp->Domain(1));
134  (*b)->SetTrimIsoFlags(tface);
135 
136 
137  // Need to use NewRuledEdge here, which means valid edges need to
138  // be created using the ellipses
139 
140  int ell1ind = (*b)->AddEdgeCurve(ellcurve1);
141  int ell2ind = (*b)->AddEdgeCurve(ellcurve2);
142  ON_BrepVertex& bottomvert1 = (*b)->NewVertex(ellcurve1->PointAt(0), SMALL_FASTF);
143  bottomvert1.m_tolerance = 0.0;
144  int vert1ind = (*b)->m_V.Count() - 1;
145  ON_BrepVertex& topvert1 = (*b)->NewVertex(ellcurve2->PointAt(0), SMALL_FASTF);
146  topvert1.m_tolerance = 0.0;
147  int vert2ind = (*b)->m_V.Count() - 1;
148  ON_BrepEdge& bottomedge = (*b)->NewEdge((*b)->m_V[vert1ind], (*b)->m_V[vert1ind], ell1ind);
149  bottomedge.m_tolerance = 0.0;
150  int bei = (*b)->m_E.Count() - 1;
151  ON_BrepEdge& topedge = (*b)->NewEdge((*b)->m_V[vert2ind], (*b)->m_V[vert2ind], ell2ind);
152  topedge.m_tolerance = 0.0;
153  int tei = (*b)->m_E.Count() - 1;
154 
155  (*b)->NewRuledFace((*b)->m_E[bei], false, (*b)->m_E[tei], false);
156 }
157 
158 
159 // Local Variables:
160 // tab-width: 8
161 // mode: C++
162 // c-basic-offset: 4
163 // indent-tabs-mode: t
164 // c-file-style: "stroustrup"
165 // End:
166 // ex: shiftwidth=4 tabstop=8
#define SMALL_FASTF
Definition: defines.h:342
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
void rt_tgc_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol)
Definition: tgc_brep.cpp:34