BRL-CAD
tor_brep.cpp
Go to the documentation of this file.
1 /* T O R _ 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  * Incorporates modified versions of openNURBS torus functions:
21  *
22  * Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
23  * OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
24  * McNeel & Associates.
25  *
26  * Permission is hereby granted, free of charge, to any person obtaining
27  * a copy of this software and associated documentation files (the
28  * "Software"), to deal in the Software without restriction, including
29  * without limitation the rights to use, copy, modify, merge, publish,
30  * distribute, sublicense, and/or sell copies of the Software.
31  *
32  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
33  * ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
34  * MERCHANTABILITY ARE HEREBY DISCLAIMED.
35  *
36  */
37 /** @file tor_brep.cpp
38  *
39  * Description - Convert torus to brep
40  *
41  */
42 
43 #include "common.h"
44 
45 #include "raytrace.h"
46 #include "rtgeom.h"
47 #include "opennurbs_torus.h"
48 
49 
50 HIDDEN ON_RevSurface*
51 Torus_RevSurfaceForm(const ON_Torus& torus )
52 {
53  ON_Circle circle = torus.MinorCircleRadians(0.0);
54  ON_ArcCurve* circle_crv = new ON_ArcCurve(circle);
55  ON_RevSurface* pRevSurface = new ON_RevSurface();
56  pRevSurface->m_angle.Set(0.0,2.0*ON_PI);
57  pRevSurface->m_t = pRevSurface->m_angle;
58  pRevSurface->m_curve = circle_crv;
59  pRevSurface->m_axis.from = torus.plane.origin;
60  pRevSurface->m_axis.to = torus.plane.origin + torus.plane.zaxis;
61  pRevSurface->m_bTransposed = false;
62  double r[2], h[2];
63  h[0] = fabs(torus.minor_radius);
64  h[1] = -h[0];
65  r[0] = fabs(torus.major_radius) + h[0];
66  r[1] = -r[0];
67  int i, j, k, n=0;
68  ON_3dPoint pt[8];
69  for (i=0;i<2;i++)
70  {
71  for (j=0;j<2;j++)
72  {
73  for (k=0;k<2;k++)
74  {
75  pt[n++] = torus.plane.PointAt( r[i], r[j], h[k] );
76  }
77  }
78  }
79  pRevSurface->m_bbox.Set( 3, 0, 8, 3, &pt[0].x );
80  return pRevSurface;
81 }
82 
83 ON_Brep*
84 Torus_Brep( const ON_Torus& torus)
85 {
86  ON_BOOL32 bArcLengthParameterization = true;
87  ON_Brep* brep = NULL;
88  ON_RevSurface* pRevSurface = Torus_RevSurfaceForm(torus);
89  if ( pRevSurface )
90  {
91  if ( bArcLengthParameterization )
92  {
93  double r = fabs(torus.major_radius);
94  if ( r <= ON_SQRT_EPSILON )
95  r = 1.0;
96  r *= ON_PI;
97  pRevSurface->SetDomain(0,0.0,2.0*r);
98  r = fabs(torus.minor_radius);
99  if ( r <= ON_SQRT_EPSILON )
100  r = 1.0;
101  r *= ON_PI;
102  pRevSurface->SetDomain(1,0.0,2.0*r);
103  }
104  brep = ON_BrepRevSurface( pRevSurface, false, false, NULL );
105  if ( !brep )
106  delete pRevSurface;
107  }
108  return brep;
109 }
110 
111 
112 extern "C" void
113 rt_tor_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *UNUSED(tol))
114 {
115  struct rt_tor_internal *tip;
116 
117  RT_CK_DB_INTERNAL(ip);
118  tip = (struct rt_tor_internal *)ip->idb_ptr;
119  RT_TOR_CK_MAGIC(tip);
120 
121  ON_3dPoint origin(tip->v);
122  ON_3dVector normal(tip->h);
123  ON_Plane p(origin, normal);
124  ON_Torus tor(p, tip->r_a, tip->r_h);
125 
126  ON_Brep *tor_brep = Torus_Brep(tor);
127  **b = *tor_brep;
128  delete tor_brep;
129 }
130 
131 
132 // Local Variables:
133 // tab-width: 8
134 // mode: C++
135 // c-basic-offset: 4
136 // indent-tabs-mode: t
137 // c-file-style: "stroustrup"
138 // End:
139 // ex: shiftwidth=4 tabstop=8
void rt_tor_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol)
Definition: tor_brep.cpp:113
Header file for the BRL-CAD common definitions.
#define HIDDEN
Definition: common.h:86
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
HIDDEN ON_RevSurface * Torus_RevSurfaceForm(const ON_Torus &torus)
Definition: tor_brep.cpp:51
#define UNUSED(parameter)
Definition: common.h:239
Support for uniform tolerances.
Definition: tol.h:71
ON_Brep * Torus_Brep(const ON_Torus &torus)
Definition: tor_brep.cpp:84
void * idb_ptr
Definition: raytrace.h:195