BRL-CAD
part_brep.cpp
Go to the documentation of this file.
1 /* P A R T _ B R E P . C P P
2  * BRL-CAD
3  *
4  * Copyright (c) 2012-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 part_brep.cpp
21  *
22  * Convert a PART (conical particle solid) 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_part_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *)
35 {
36  struct rt_part_internal *pip;
37 
39  pip = (struct rt_part_internal *)ip->idb_ptr;
40  RT_PART_CK_MAGIC(pip);
41 
42  fastf_t temp = asin((pip->part_vrad-pip->part_hrad)/MAGNITUDE(pip->part_H));
43  point_t origin;
44  VSET(origin, 0.0, 0.0, 0.0);
45 
46  // First, create the body of the particle. It should be a revolution of a line.
47  // The body should be either a cylinder or a truncated cone which is tangent to
48  // the two hemispheres.
49  point_t VaddH;
50  vect_t r;
51  VADD2(VaddH, pip->part_V, pip->part_H);
52  bn_vec_ortho(r, pip->part_H);
53  VUNITIZE(r);
54  point_t startpoint, endpoint;
55  VJOIN2(startpoint, pip->part_V, pip->part_vrad*cos(temp), r, sin(temp)*pip->part_vrad/MAGNITUDE(pip->part_H), pip->part_H);
56  VJOIN2(endpoint, VaddH, pip->part_hrad*cos(temp), r, sin(temp)*pip->part_hrad/MAGNITUDE(pip->part_H), pip->part_H);
57  ON_Line line = ON_Line(ON_3dPoint(startpoint), ON_3dPoint(endpoint));
58  ON_LineCurve *lcurve = new ON_LineCurve(line);
59 
60  point_t revpoint1, revpoint2;
61  VMOVE(revpoint1, pip->part_V);
62  VMOVE(revpoint2, VaddH);
63  ON_3dPoint rpnt1 = ON_3dPoint(revpoint1);
64  ON_3dPoint rpnt2 = ON_3dPoint(revpoint2);
65  ON_Line revaxis(rpnt1, rpnt2);
66 
67  ON_RevSurface* part_surf = ON_RevSurface::New();
68  part_surf->m_curve = lcurve;
69  part_surf->m_axis = revaxis;
70  part_surf->m_angle = ON_Interval(0, 2*ON_PI);
71  ON_NurbsSurface *part_nurbs_surf = ON_NurbsSurface::New();
72  part_surf->GetNurbForm(*part_nurbs_surf, 0.0);
73  delete part_surf;
74 
75  if (!EQUAL(fabs(temp), ON_PI/2.0)) {
76  (*b)->m_S.Append(part_nurbs_surf);
77  int surfindex = (*b)->m_S.Count();
78  ON_BrepFace& face = (*b)->NewFace(surfindex - 1);
79  (*b)->FlipFace(face);
80  int faceindex = (*b)->m_F.Count();
81  (*b)->NewOuterLoop(faceindex-1);
82  }
83 
84  // Then, create the v-hemisphere.
85  vect_t minusH;
86  VREVERSE(minusH, pip->part_H);
87  ON_Plane vplane = ON_Plane(ON_3dPoint(origin), ON_3dPoint(minusH), ON_3dPoint(r));
88  ON_Circle vcircle = ON_Circle(vplane, pip->part_vrad);
89  ON_NurbsCurve *v_curve = ON_NurbsCurve::New();
90  const ON_Interval subinterval = ON_Interval(0, ON_PI/2.0 + temp);
91  ON_Arc v_arc(vcircle, subinterval);
92  v_arc.Translate(ON_3dVector(pip->part_V));
93  v_arc.GetNurbForm(*v_curve);
94  ON_RevSurface* v_sph_surf = ON_RevSurface::New();
95  v_sph_surf->m_curve = v_curve;
96  v_sph_surf->m_axis = revaxis;
97  v_sph_surf->m_angle = ON_Interval(0, 2*ON_PI);
98  ON_NurbsSurface *v_nurbs_surf = ON_NurbsSurface::New();
99  v_sph_surf->GetNurbForm(*v_nurbs_surf, 0.0);
100  delete v_sph_surf;
101 
102  if (!EQUAL(temp, -ON_PI/2.0)) {
103  (*b)->m_S.Append(v_nurbs_surf);
104  int surfindex = (*b)->m_S.Count();
105  ON_BrepFace& face = (*b)->NewFace(surfindex - 1);
106  (*b)->FlipFace(face);
107  int faceindex = (*b)->m_F.Count();
108  (*b)->NewOuterLoop(faceindex-1);
109  }
110 
111  // Last, the h-hemisphere.
112  ON_Plane hplane = ON_Plane(ON_3dPoint(origin), ON_3dPoint(pip->part_H), ON_3dPoint(r));
113  ON_Circle hcircle = ON_Circle(hplane, pip->part_hrad);
114  ON_NurbsCurve *h_curve = ON_NurbsCurve::New();
115  const ON_Interval subinterval2 = ON_Interval(ON_PI/2.0 - temp, 0);
116  ON_Arc h_arc(hcircle, subinterval2);
117  h_arc.Translate(ON_3dVector(VaddH));
118  h_arc.GetNurbForm(*h_curve);
119  ON_RevSurface* h_sph_surf = ON_RevSurface::New();
120  h_sph_surf->m_curve = h_curve;
121  h_sph_surf->m_axis = revaxis;
122  h_sph_surf->m_angle = ON_Interval(0, 2*ON_PI);
123  ON_NurbsSurface *h_nurbs_surf = ON_NurbsSurface::New();
124  h_sph_surf->GetNurbForm(*h_nurbs_surf, 0.0);
125  delete h_sph_surf;
126 
127  if (!EQUAL(temp, ON_PI/2.0)) {
128  (*b)->m_S.Append(h_nurbs_surf);
129  int surfindex = (*b)->m_S.Count();
130  ON_BrepFace& face2 = (*b)->NewFace(surfindex - 1);
131  (*b)->FlipFace(face2);
132  int faceindex = (*b)->m_F.Count();
133  (*b)->NewOuterLoop(faceindex-1);
134  }
135 }
136 
137 
138 // Local Variables:
139 // tab-width: 8
140 // mode: C++
141 // c-basic-offset: 4
142 // indent-tabs-mode: t
143 // c-file-style: "stroustrup"
144 // End:
145 // ex: shiftwidth=4 tabstop=8
#define VSET(a, b, c, d)
Definition: color.c:53
Header file for the BRL-CAD common definitions.
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
Support for uniform tolerances.
Definition: tol.h:71
void * idb_ptr
Definition: raytrace.h:195
void bn_vec_ortho(vect_t out, const vect_t in)
void rt_part_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *)
Definition: part_brep.cpp:34
double fastf_t
Definition: defines.h:300