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