Panda3D
lwoDiscontinuousVertexMap.cxx
1 // Filename: lwoDiscontinuousVertexMap.cxx
2 // Created by: drose (24Apr01)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "lwoDiscontinuousVertexMap.h"
16 #include "lwoInputFile.h"
17 
18 #include "dcast.h"
19 #include "indent.h"
20 
21 #include <algorithm>
22 
23 TypeHandle LwoDiscontinuousVertexMap::_type_handle;
24 
25 
26 ////////////////////////////////////////////////////////////////////
27 // Function: LwoDiscontinuousVertexMap::has_value
28 // Access: Public
29 // Description: Returns true if the map has a value associated with
30 // the given index, false otherwise.
31 ////////////////////////////////////////////////////////////////////
33 has_value(int polygon_index, int vertex_index) const {
34  VMad::const_iterator di;
35  di = _vmad.find(polygon_index);
36  if (di != _vmad.end()) {
37  const VMap &vmap = (*di).second;
38  return (vmap.count(vertex_index) != 0);
39  }
40 
41  return false;
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: LwoDiscontinuousVertexMap::get_value
46 // Access: Public
47 // Description: Returns the mapping value associated with the given
48 // index, or an empty PTA_stdfloat if there is no mapping
49 // value associated.
50 ////////////////////////////////////////////////////////////////////
51 PTA_stdfloat LwoDiscontinuousVertexMap::
52 get_value(int polygon_index, int vertex_index) const {
53  VMad::const_iterator di;
54  di = _vmad.find(polygon_index);
55  if (di != _vmad.end()) {
56  const VMap &vmap = (*di).second;
57  VMap::const_iterator vi;
58  vi = vmap.find(vertex_index);
59  if (vi != vmap.end()) {
60  return (*vi).second;
61  }
62  }
63 
64  return PTA_stdfloat();
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: LwoDiscontinuousVertexMap::read_iff
69 // Access: Public, Virtual
70 // Description: Reads the data of the chunk in from the given input
71 // file, if possible. The ID and length of the chunk
72 // have already been read. stop_at is the byte position
73 // of the file to stop at (based on the current position
74 // at in->get_bytes_read()). Returns true on success,
75 // false otherwise.
76 ////////////////////////////////////////////////////////////////////
78 read_iff(IffInputFile *in, size_t stop_at) {
79  LwoInputFile *lin = DCAST(LwoInputFile, in);
80 
81  _map_type = lin->get_id();
82  _dimension = lin->get_be_uint16();
83  _name = lin->get_string();
84 
85  while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
86  int vertex_index = lin->get_vx();
87  int polygon_index = lin->get_vx();
88 
89  PTA_stdfloat value;
90  for (int i = 0; i < _dimension; i++) {
91  value.push_back(lin->get_be_float32());
92  }
93 
94  VMap &vmap = _vmad[polygon_index];
95  pair<VMap::iterator, bool> ir =
96  vmap.insert(VMap::value_type(vertex_index, value));
97  if (!ir.second) {
98  // This polygon/vertex pair was repeated in the vmad. Is it
99  // simply redundant, or is it contradictory?
100  PTA_stdfloat orig_value = (*ir.first).second;
101 
102  if (value.v() != orig_value.v()) {
103  nout << "Multiple UV values for vertex " << vertex_index
104  << " of polygon " << polygon_index
105  << " specified by discontinuous vertex map.\n"
106  << "Original value = ";
107 
108  PTA_stdfloat::const_iterator vi;
109  for (vi = orig_value.begin(); vi != orig_value.end(); ++vi) {
110  nout << (*vi) << " ";
111  }
112  nout << " new value = ";
113  for (vi = value.begin(); vi != value.end(); ++vi) {
114  nout << (*vi) << " ";
115  }
116  nout << "\n";
117  }
118  }
119  }
120 
121  return (lin->get_bytes_read() == stop_at);
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: LwoDiscontinuousVertexMap::write
126 // Access: Public, Virtual
127 // Description:
128 ////////////////////////////////////////////////////////////////////
129 void LwoDiscontinuousVertexMap::
130 write(ostream &out, int indent_level) const {
131  indent(out, indent_level)
132  << get_id() << " { map_type = " << _map_type
133  << ", dimension = " << _dimension
134  << ", name = \"" << _name << "\", "
135  << _vmad.size() << " polygons }\n";
136 }
PTA_stdfloat get_value(int polygon_index, int vertex_index) const
Returns the mapping value associated with the given index, or an empty PTA_stdfloat if there is no ma...
int get_vx()
Reads a Lightwave variable-length index.
string get_string()
Extracts a null-terminated string.
size_t get_bytes_read() const
Returns the number of bytes read so far from the input file.
Definition: iffInputFile.I:56
PN_uint16 get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
bool has_value(int polygon_index, int vertex_index) const
Returns true if the map has a value associated with the given index, false otherwise.
virtual bool read_iff(IffInputFile *in, size_t stop_at)
Reads the data of the chunk in from the given input file, if possible.
A wrapper around an istream used for reading an IFF file.
Definition: iffInputFile.h:33
PN_stdfloat get_be_float32()
Extracts a 32-bit big-endian single-precision floating-point number.
A specialization of IffInputFile to handle reading a Lightwave Object file.
Definition: lwoInputFile.h:29
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
IffId get_id() const
Returns the ID associated with this chunk.
Definition: iffChunk.I:31
bool is_eof() const
Returns true if the last read operation failed because of reaching EOF, false otherwise.
Definition: iffInputFile.I:45
IffId get_id()
Extracts a 4-character IFF ID.