Developer Documentation
BSplineWriters.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #include "FileOBJ.hh"
51 
52 
53 #ifdef ENABLE_BSPLINECURVE_SUPPORT
54 bool FileOBJPlugin::writeCurve(std::ostream& _out, QString _filename, BSplineCurve* _curve )
55 {
56  if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0) {
57  _out.precision(savePrecision_->value());
58  }
59 
60  _out << "# " << _filename.toStdString() << "\n";
61 
62  // save control points (coordinates)
63  for (uint i = 0; i < _curve->n_control_points(); ++i){
64  ACG::Vec3d cp = _curve->get_control_point(i);
65  _out << "v " << cp[0] << " " << cp[1] << " " << cp[2] << "\n";
66  }
67 
68  _out << "cstype bspline\n";
69  _out << "deg " << _curve->degree() << "\n";
70  _out << "g " << _filename.toStdString() << "\n";
71 
72  // save control polygon
73  _out << "curv " << _curve->get_knot(0) << " " << _curve->get_knot(_curve->n_knots()-1) << " ";
74  // save control point indices
75  for (unsigned int i = 0; i < _curve->n_control_points(); ++i)
76  _out << i+1 << " "; // obj enumerates the cps starting with 1
77  _out << "\n";
78 
79  _out << "parm u ";
80  // save knotvector
81  for (unsigned int i = 0; i < _curve->n_knots(); ++i)
82  _out << _curve->get_knot(i) << " ";
83  _out << "\n";
84 
85  _out << "end";
86 
87  return true;
88 }
89 #endif
90 
91 #ifdef ENABLE_BSPLINESURFACE_SUPPORT
92 bool FileOBJPlugin::writeSurface(std::ostream& _out, QString _filename, BSplineSurface* _surface ){
93  if ( !OpenFlipper::Options::savingSettings() && saveOptions_ != 0) {
94  _out.precision(savePrecision_->value());
95  }
96 
97  _out << "# " << _filename.toStdString() << "\n";
98 
99  // save control net (coordinates)
100  unsigned int num_cp_m = _surface->n_control_points_m();
101  unsigned int num_cp_n = _surface->n_control_points_n();
102 
103  for (unsigned int i = 0; i < num_cp_m; ++i)
104  {
105  for (unsigned int j = 0; j < num_cp_n; ++j)
106  {
107  ACG::Vec3d cp = (*_surface)(i,j);
108  _out << "v " << cp[0] << " " << cp[1] << " " << cp[2] << "\n";
109  }
110  }
111 
112  _out << "cstype bspline\n";
113  _out << "deg " << _surface->degree_m() << " " << _surface->degree_n() << "\n";
114  _out << "g " << _filename.toStdString() << "\n";
115 
116  // save control polygon
117  _out << "surf " << _surface->get_knot_m(0) << " " << _surface->get_knot_m(_surface->n_knots_m()-1) << " "
118  << _surface->get_knot_n(0) << " " << _surface->get_knot_n(_surface->n_knots_n()-1) << " ";
119 
120  // save control point indices
121  for (unsigned int j = 0; j < num_cp_n; ++j)
122  for (unsigned int i = 0; i < num_cp_m; ++i)
123  _out << (i*num_cp_n) + j+1 << " "; // obj enumerates the cps starting with 1
124 
125  _out << "\n";
126 
127  _out << "parm u ";
128  // save knotvector in m direction
129  for (unsigned int i = 0; i < _surface->n_knots_m(); ++i)
130  _out << _surface->get_knot_m(i) << " ";
131  _out << "\n";
132 
133  _out << "parm v ";
134  // save knotvector in n direction
135  for (unsigned int i = 0; i < _surface->n_knots_n(); ++i)
136  _out << _surface->get_knot_n(i) << " ";
137  _out << "\n";
138 
139  _out << "end";
140 
141  return true;
142 }
143 
144 #endif
145 
unsigned int n_knots_m()
Returns the number of knots in m direction.
int degree_n() const
Returns the spline degree in n direction.
Scalar get_knot(int _i)
get knot i
Point & get_control_point(int _i)
get control point i
unsigned int n_control_points() const
Returns the number of control points.
unsigned int n_knots() const
Returns the number of knots.
unsigned int n_control_points_m() const
Returns the number of controlpoints in m direction.
Scalar get_knot_n(int _i)
Get knot i in n direction.
unsigned int degree() const
Returns the spline degree.
Scalar get_knot_m(int _i)
Get knot i in m direction.
int degree_m() const
Returns the spline degree in m direction.
unsigned int n_control_points_n() const
Returns the number of controlpoints in n direction.
unsigned int n_knots_n()
Returns the number of knots in n direction.