BRL-CAD
ell_brep.cpp
Go to the documentation of this file.
1 /* E L L _ 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 ell_brep.cpp
21  *
22  * Convert a Generalized Ellipsoid 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 extern "C" void
33 rt_ell_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol)
34 {
35  struct rt_ell_internal *eip;
36 
38  eip = (struct rt_ell_internal *)ip->idb_ptr;
39  RT_ELL_CK_MAGIC(eip);
40 
41  // For Algorithms used in rotation and translation
42  // Please refer to ell.c
43  // X' = S(R(X - V))
44  // X = invR(invS(X')) + V = invRinvS(X') + V;
45  // where R(X) = (A/(|A|))
46  // (B/(|B|)) . X
47  // (C/(|C|))
48  //
49  // and S(X) = (1/|A| 0 0 )
50  // (0 1/|B| 0 ) . X
51  // (0 0 1/|C|)
52 
53  // Parameters for Rotate and Translate
54  vect_t Au, Bu, Cu; /* A, B, C with unit length */
55  mat_t R;
56  mat_t Rinv;
57  mat_t Sinv;
58  mat_t invRinvS;
59  fastf_t magsq_a, magsq_b, magsq_c;
60  fastf_t f;
61 
62  magsq_a = MAGSQ(eip->a);
63  magsq_b = MAGSQ(eip->b);
64  magsq_c = MAGSQ(eip->c);
65 
66  if (magsq_a < tol->dist_sq || magsq_b < tol->dist_sq || magsq_c < tol->dist_sq) {
67  bu_log("rt_ell_brep(): ell zero length A(%g), B(%g), or C(%g) vector\n",
68  magsq_a, magsq_b, magsq_c);
69  }
70 
71  f = 1.0/sqrt(magsq_a);
72  VSCALE(Au, eip->a, f);
73  f = 1.0/sqrt(magsq_b);
74  VSCALE(Bu, eip->b, f);
75  f = 1.0/sqrt(magsq_c);
76  VSCALE(Cu, eip->c, f);
77 
78  MAT_IDN(R);
79  VMOVE(&R[0], Au);
80  VMOVE(&R[4], Bu);
81  VMOVE(&R[8], Cu);
82  bn_mat_trn(Rinv, R);
83 
84  MAT_IDN(Sinv);
85  Sinv[0] = MAGNITUDE(eip->a);
86  Sinv[5] = MAGNITUDE(eip->b);
87  Sinv[10] = MAGNITUDE(eip->c);
88  bn_mat_mul(invRinvS, Rinv, Sinv);
89 
90  point_t origin;
91  VSET(origin, 0, 0, 0);
92  ON_Sphere sph(origin, 1);
93 
94  // Get the NURBS form of the surface
95  ON_NurbsSurface *ellcurvedsurf = ON_NurbsSurface::New();
96  sph.GetNurbForm(*ellcurvedsurf);
97 
98  // Scale, rotate and translate control points
99  for (int i = 0; i < ellcurvedsurf->CVCount(0); i++) {
100  for (int j = 0; j < ellcurvedsurf->CVCount(1); j++) {
101  point_t cvpt;
102  ON_4dPoint ctrlpt;
103  ellcurvedsurf->GetCV(i, j, ctrlpt);
104  MAT3X3VEC(cvpt, invRinvS, ctrlpt);
105  point_t scale_v;
106  VSCALE(scale_v, eip->v, ctrlpt.w);
107  VADD2(cvpt, scale_v, cvpt);
108  ON_4dPoint newpt = ON_4dPoint(cvpt[0], cvpt[1], cvpt[2], ctrlpt.w);
109  ellcurvedsurf->SetCV(i, j, newpt);
110  }
111  }
112 
113  ellcurvedsurf->SetDomain(0, 0.0, 1.0);
114  ellcurvedsurf->SetDomain(1, 0.0, 1.0);
115 
116  // Make final BREP structure
117  (*b)->m_S.Append(ellcurvedsurf);
118  int surfindex = (*b)->m_S.Count();
119  (*b)->NewFace(surfindex - 1);
120  int faceindex = (*b)->m_F.Count();
121  (*b)->NewOuterLoop(faceindex-1);
122 }
123 
124 
125 // Local Variables:
126 // tab-width: 8
127 // mode: C++
128 // c-basic-offset: 4
129 // indent-tabs-mode: t
130 // c-file-style: "stroustrup"
131 // End:
132 // ex: shiftwidth=4 tabstop=8
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define VSET(a, b, c, d)
Definition: color.c:53
Header file for the BRL-CAD common definitions.
void rt_ell_brep(ON_Brep **b, const struct rt_db_internal *ip, const struct bn_tol *tol)
Definition: ell_brep.cpp:33
#define RT_CK_DB_INTERNAL(_p)
Definition: raytrace.h:207
void bn_mat_trn(mat_t om, const mat_t im)
void bn_mat_mul(mat_t o, const mat_t a, const mat_t b)
Support for uniform tolerances.
Definition: tol.h:71
void * idb_ptr
Definition: raytrace.h:195
#define R
Definition: msr.c:55
double fastf_t
Definition: defines.h:300