PolyLineObjectSerializer.cc 11.5 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision: 21016 $                                                       *
*   $LastChangedBy: schultz $                                                *
*   $Date: 2015-07-16 16:48:42 +0200 (Do, 16 Jul 2015) $                     *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include "PolyLineObjectSerializer.hh"

#include <ObjectTypes/PolyLine/PolyLineCircleData.hh>
#include <ObjectTypes/PolyLine/PolyLineBezierSplineData.hh>

PolyLineObjectSerializer::PolyLineObjectSerializer(PolyLineObject* _object)
    : instance(_object)
{
}

void PolyLineObjectSerializer::serialize(std::ostream& _stream) {
    std::ostream& fout = _stream;
    PolyLine& _polyLine = *instance->line();
    //  is polyline closed?
    fout << _polyLine.is_closed() << std::endl;

    // number of points
    fout << _polyLine.n_vertices() << std::endl;

    //std::cerr << "write " << _filename << std::endl;
    std::cerr << "#points: " << _polyLine.n_vertices() << std::endl;

72
    fout.precision(14);
Jan Möbius's avatar
Jan Möbius committed
73 74 75 76 77 78 79 80 81 82 83

    // write each point
    for(unsigned int i=0; i< _polyLine.n_vertices(); ++i)
    {

    const PolyLine::Point& current_point = _polyLine.point(i);
    fout << current_point[0] << " ";
    fout << current_point[1] << " ";
    fout << current_point[2] << std::endl;
    }

84 85 86 87
    fout << "VERTEXRADIUS" << std::endl << _polyLine.vertex_radius() << std::endl;
    fout << "EDGERADIUS" << std::endl << _polyLine.edge_radius() << std::endl;


Jan Möbius's avatar
Jan Möbius committed
88 89 90 91 92
    // ###########################
    // Write properties(unsigned int)CircleType

    if(_polyLine.vertex_vhandles_available())
    {
Ellen Dekkers's avatar
Ellen Dekkers committed
93 94 95
      fout << "VVHANDLES" << std::endl;
      for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
        fout << _polyLine.vertex_vhandle(i) << std::endl;
Jan Möbius's avatar
Jan Möbius committed
96 97 98 99
    }

    if(_polyLine.vertex_ehandles_available())
    {
Ellen Dekkers's avatar
Ellen Dekkers committed
100 101 102
      fout << "VEHANDLES" << std::endl;
      for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
        fout << _polyLine.vertex_ehandle(i) << std::endl;
Jan Möbius's avatar
Jan Möbius committed
103 104 105 106
    }

    if(_polyLine.vertex_fhandles_available())
    {
Ellen Dekkers's avatar
Ellen Dekkers committed
107 108 109
      fout << "VFHANDLES" << std::endl;
      for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
        fout << _polyLine.vertex_fhandle(i) << std::endl;
Jan Möbius's avatar
Jan Möbius committed
110 111
    }

Ellen Dekkers's avatar
Ellen Dekkers committed
112
    if(_polyLine.vertex_scalars_available())
Jan Möbius's avatar
Jan Möbius committed
113
    {
Ellen Dekkers's avatar
Ellen Dekkers committed
114 115 116
      fout << "VSCALARS" << std::endl;
      for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
        fout << _polyLine.vertex_scalar(i) << std::endl;
Jan Möbius's avatar
Jan Möbius committed
117
    }
Ellen Dekkers's avatar
Ellen Dekkers committed
118 119 120 121 122 123 124 125 126 127 128

    if(_polyLine.vertex_normals_available())
    {
      fout << "VNORMALS" << std::endl;
      for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
      {
        const PolyLine::Point& current_vnormal = _polyLine.vertex_normal(i);
        fout << current_vnormal[0] << " ";
        fout << current_vnormal[1] << " ";
        fout << current_vnormal[2] << std::endl;
      }
Jan Möbius's avatar
Jan Möbius committed
129 130 131 132
    }

    if(_polyLine.vertex_binormals_available())
    {
Ellen Dekkers's avatar
Ellen Dekkers committed
133 134 135 136 137 138 139 140
      fout << "VBINORMALS" << std::endl;
      for( unsigned int i=0; i<_polyLine.n_vertices(); ++i)
      {
        const PolyLine::Point& current_bnormal = _polyLine.vertex_binormal(i);
        fout << current_bnormal[0] << " ";
        fout << current_bnormal[1] << " ";
        fout << current_bnormal[2] << std::endl;
      }
Jan Möbius's avatar
Jan Möbius committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    }

    PolyLineCircleData* circleData = dynamic_cast<PolyLineCircleData*>(instance->objectData(CIRCLE_DATA));
    if(circleData) {
      fout << "CIRCLEDATA" << std::endl;
      fout << circleData->circleCenter_ << std::endl;
      fout << circleData->circleMainAxis_ << std::endl;
      fout << circleData->circleMainRadius_ << std::endl;
      fout << circleData->circleNormal_ << std::endl;
      fout << circleData->circleSideAxis_ << std::endl;
      fout << circleData->circleSideRadius_ << std::endl;
    }

    PolyLineBezierSplineData* splineData = dynamic_cast<PolyLineBezierSplineData*>(instance->objectData(BEZSPLINE_DATA));
    if(splineData) {
Ellen Dekkers's avatar
Ellen Dekkers committed
156 157 158 159 160 161 162 163 164 165
      fout << "SPLINEDATA" << std::endl;
      fout << (unsigned int)splineData->points_.size() << std::endl;
      for(unsigned int i = 0; i < splineData->points_.size(); i++) {
        fout << splineData->points_[i].normal << std::endl;
        fout << splineData->points_[i].position << std::endl;
      }
      fout << (unsigned int)splineData->handles_.size() << std::endl;
      for(unsigned int i = 0; i < splineData->handles_.size(); i++)
        fout << splineData->handles_[i] << std::endl;
      fout << std::endl;
Jan Möbius's avatar
Jan Möbius committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    }
}

void PolyLineObjectSerializer::deserialize(std::istream& _stream) {
    std::istream& fin = _stream;
    PolyLine& _polyLine = *instance->line();

    // clear old polyline
    _polyLine.clear();
    // closed ?
    bool closed = false;
    fin >> closed;

    _polyLine.set_closed(closed);

    // number of points
    int num_points;
    fin >> num_points;

    // read points
    for(int i=0; i<num_points; ++i)
    {
      PolyLine::Point::value_type x,y,z;
      fin >> x;
      fin >> y;
      fin >> z;
      PolyLine::Point p(x,y,z);
      _polyLine.add_point(p);
    }

    // ###########################
    // READ properties

    std::string token;
    while(true)
    {
      token = "-";
      fin >> token;
204 205 206 207 208 209 210 211 212 213 214 215 216
      if(token == "VERTEXRADIUS")
      {
        double r;
        fin >> r;
        _polyLine.set_vertex_radius(r);
      }
      else if(token == "EDGERADIUS")
      {
        double r;
        fin >> r;
        _polyLine.set_edge_radius(r);
      }
      else if(token == "VVHANDLES")
Jan Möbius's avatar
Jan Möbius committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
      {
        if(!_polyLine.vertex_vhandles_available()) _polyLine.request_vertex_vhandles();
        for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
          fin >> _polyLine.vertex_vhandle(i);
      }
      else if(token == "VEHANDLES")
      {
        if(!_polyLine.vertex_ehandles_available()) _polyLine.request_vertex_ehandles();
        for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
          fin >> _polyLine.vertex_ehandle(i);
      }
      else if(token == "VFHANDLES")
      {
        if(!_polyLine.vertex_fhandles_available()) _polyLine.request_vertex_fhandles();
        for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
          fin >> _polyLine.vertex_fhandle(i);
      }
Ellen Dekkers's avatar
Ellen Dekkers committed
234 235 236 237 238 239
      else if(token == "VSCALARS")
      {
        if(!_polyLine.vertex_scalars_available()) _polyLine.request_vertex_scalars();
        for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
          fin >> _polyLine.vertex_scalar(i);
      }
Jan Möbius's avatar
Jan Möbius committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
      else if(token == "VNORMALS")
      {
        if(!_polyLine.vertex_normals_available()) _polyLine.request_vertex_normals();
        for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
        {
          fin >> _polyLine.vertex_normal(i)[0];
          fin >> _polyLine.vertex_normal(i)[1];
          fin >> _polyLine.vertex_normal(i)[2];
        }
      }
      else if(token == "VBINORMALS")
      {
        if(!_polyLine.vertex_binormals_available()) _polyLine.request_vertex_binormals();
        for(unsigned int i=0; i<_polyLine.n_vertices(); ++i)
        {
          fin >> _polyLine.vertex_binormal(i)[0];
          fin >> _polyLine.vertex_binormal(i)[1];
          fin >> _polyLine.vertex_binormal(i)[2];
        }
      }
      else if(token == "CIRCLEDATA") {
          ACG::Vec3d center, normal, main, side;
          double rmain, rside;
          fin >> center;
          fin >> main;
          fin >> rmain;
          fin >> normal;
          fin >> side;
          fin >> rside;
          PolyLineCircleData* circleData = new PolyLineCircleData(center,normal,main,side,rmain,rside,std::numeric_limits<unsigned int>::max());
          instance->setObjectData(CIRCLE_DATA, circleData);
      }
      else if(token == "SPLINEDATA") {
          PolyLineBezierSplineData* splineData = new PolyLineBezierSplineData(std::numeric_limits<unsigned int>::max());
          unsigned int pointCount, handleCount;
          fin >> pointCount;
          PolyLineBezierSplineData::InterpolatePoint point;
          for(unsigned int i = 0; i < pointCount; i++) {
              fin >> point.normal;
              fin >> point.position;
              splineData->points_.push_back(point);
          }
          fin >> handleCount;
          for(unsigned int i = 0; i < handleCount; i++) {
              fin >> point.position;
              splineData->handles_.push_back(point.position);
          }
          instance->setObjectData(BEZSPLINE_DATA, splineData);
      }
      else if(token != "") break; //eat up empty lines
    }
291

Jan Möbius's avatar
Jan Möbius committed
292 293 294 295 296 297
}