summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/prc/PRCTools/makePRC.cc
blob: f757300020397ecc1fb71942794031da69ac4954 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/************
*
*   This file is part of a tool for producing 3D content in the PRC format.
*   Copyright (C) 2008  Orest Shardt <shardtor (at) gmail dot com>
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU Lesser General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU Lesser General Public License for more details.
*
*   You should have received a copy of the GNU Lesser General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*************/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <unistd.h>
#include <cstdlib>
#include "../oPRCFile.h"

using namespace std;

void readPoints(istream &is, unsigned int n, double p[][3])
{
  for(unsigned int i = 0; i < n; ++i)
  {
    is >> p[i][0] >> p[i][1] >> p[i][2];
  }
  if(!is)
  {
    cerr << "Error reading list of points." << endl;
    exit(1);
  }
}

void readDoubles(istream &is, unsigned int n, double d[])
{
  for(unsigned int i = 0; i < n; ++i)
  {
    is >> d[i];
  }
  if(!is)
  {
    cerr << "Error reading list of doubles." << endl;
    exit(1);
  }
}

int main(int argc, char **argv)
{
  const char *oFileName = "output.prc";

  int c;
  opterr = 0;
  while ((c = getopt (argc, argv, "o:")) != -1)
    switch (c)
    {
      case 'o':
        oFileName = optarg;
        break;
      case '?':
        if (optopt == 'o')
          cerr << "Option '-o' requires an argument, the filename." << endl;
        else
          cerr << "Unrecognized option '-" << (char)optopt << "'." << endl;
        exit(1);
        break;
      default:
        exit(1);
    }
  istream *ins = NULL;
  if(optind < argc)
  {
    ins = new ifstream(argv[optind]);
    if(!*ins)
    {
      cerr << "Error opening input file " << argv[optind] << endl;
      exit(1);
    }
  }
  else
  {
    ins = &cin;
  }

  ofstream outf(oFileName);
  if(!outf)
  {
    cerr << "Error opening output file " << oFileName << endl;
    exit(1);
  }

  oPRCFile oPRC(outf);

  string entityType;
  while(*ins)
  {
    *ins >> entityType;
    if(!*ins) break;
    if(entityType == "Line")
    {
      double r,g,b,a;
      unsigned int numberOfPoints;
      *ins >> r >> g >> b >> a >> numberOfPoints;
      if(!*ins)
      {
        cerr << "Error reading line data." << endl;
        exit(1);
      }
      else
      {
        double (*points)[3] = new double[numberOfPoints][3];
        readPoints(*ins,numberOfPoints,points);
        oPRC.add(new PRCline(&oPRC,numberOfPoints,points,
                 *new RGBAColour(r,g,b,a)));
      }
    }
    else if(entityType == "Curve")
    {
      double r,g,b,a;
      unsigned int numberOfPoints;
      unsigned int degree;
      *ins >> r >> g >> b >> a >> degree >> numberOfPoints;
      if(!*ins)
      {
        cerr << "Error reading curve data." << endl;
        exit(1);
      }
      else
      {
        double (*points)[3] = new double[numberOfPoints][3];
        double *knots = new double[degree+numberOfPoints+1];
        readPoints(*ins,numberOfPoints,points);
        readDoubles(*ins,degree+numberOfPoints+1,knots);
        oPRC.add(new PRCcurve(&oPRC,degree,numberOfPoints,points,
                 knots,*new RGBAColour(r,g,b,a)));
      }
    }
    else if(entityType == "Surface")
    {
      double r,g,b,a;
      unsigned int numberOfPointsU,numberOfPointsV;
      unsigned int degreeU,degreeV;
      *ins >> r >> g >> b >> a >> degreeU >> degreeV >> numberOfPointsU
          >> numberOfPointsV;
      if(!*ins)
      {
        cerr << "Error reading surface data." << endl;
        exit(1);
      }
      else
      {
        double (*points)[3] = new double[numberOfPointsU*numberOfPointsV][3];
        double *knotsU = new double[degreeU+numberOfPointsU+1];
        double *knotsV = new double[degreeV+numberOfPointsV+1];
        readPoints(*ins,numberOfPointsU*numberOfPointsV,points);
        readDoubles(*ins,degreeU+numberOfPointsU+1,knotsU);
        readDoubles(*ins,degreeV+numberOfPointsV+1,knotsV);
        oPRC.add(new PRCsurface(&oPRC,degreeU,degreeV,numberOfPointsU,
                 numberOfPointsV,points,knotsU,knotsV,
                 *new RGBAColour(r,g,b,a)));
      }
    }
    else
    {
      cerr << "Unrecognized entity type " << entityType << endl;
      exit(1);
    }
  }

  if(ins && ins != &cin)
    delete ins;


  oPRC.finish();

  outf.close();

  return 0;
}