Panda3D
lwoVertexMap.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 lwoVertexMap.cxx
10  * @author drose
11  * @date 2001-04-24
12  */
13 
14 #include "lwoVertexMap.h"
15 #include "lwoInputFile.h"
16 
17 #include "dcast.h"
18 #include "indent.h"
19 
20 TypeHandle LwoVertexMap::_type_handle;
21 
22 
23 /**
24  * Returns true if the map has a value associated with the given index, false
25  * otherwise.
26  */
27 bool LwoVertexMap::
28 has_value(int index) const {
29  return (_vmap.count(index) != 0);
30 }
31 
32 /**
33  * Returns the mapping value associated with the given index, or an empty
34  * PTA_stdfloat if there is no mapping value associated.
35  */
36 PTA_stdfloat LwoVertexMap::
37 get_value(int index) const {
38  VMap::const_iterator vi;
39  vi = _vmap.find(index);
40  if (vi != _vmap.end()) {
41  return (*vi).second;
42  }
43 
44  return PTA_stdfloat();
45 }
46 
47 /**
48  * Reads the data of the chunk in from the given input file, if possible. The
49  * ID and length of the chunk have already been read. stop_at is the byte
50  * position of the file to stop at (based on the current position at
51  * in->get_bytes_read()). Returns true on success, false otherwise.
52  */
53 bool LwoVertexMap::
54 read_iff(IffInputFile *in, size_t stop_at) {
55  LwoInputFile *lin = DCAST(LwoInputFile, in);
56 
57  _map_type = lin->get_id();
58  _dimension = lin->get_be_uint16();
59  _name = lin->get_string();
60 
61  while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
62  int index = lin->get_vx();
63 
64  PTA_stdfloat value;
65  for (int i = 0; i < _dimension; i++) {
66  value.push_back(lin->get_be_float32());
67  }
68 
69  bool inserted = _vmap.insert(VMap::value_type(index, value)).second;
70  if (!inserted) {
71  nout << "Duplicate index " << index << " in map.\n";
72  }
73  }
74 
75  return (lin->get_bytes_read() == stop_at);
76 }
77 
78 /**
79  *
80  */
81 void LwoVertexMap::
82 write(std::ostream &out, int indent_level) const {
83  indent(out, indent_level)
84  << get_id() << " { map_type = " << _map_type
85  << ", dimension = " << _dimension
86  << ", name = \"" << _name << "\", "
87  << _vmap.size() << " values }\n";
88 }
uint16_t get_be_uint16()
Extracts an unsigned 16-bit big-endian integer.
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
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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
PTA_stdfloat get_value(int index) const
Returns the mapping value associated with the given index, or an empty PTA_stdfloat if there is no ma...
bool has_value(int 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.
virtual bool read_iff(IffInputFile *in, size_t stop_at)
Reads the data of the chunk in from the given input file, if possible.
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.