Panda3D
lwoVertexMap.cxx
1 // Filename: lwoVertexMap.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 "lwoVertexMap.h"
16 #include "lwoInputFile.h"
17 
18 #include "dcast.h"
19 #include "indent.h"
20 
21 TypeHandle LwoVertexMap::_type_handle;
22 
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: LwoVertexMap::has_value
26 // Access: Public
27 // Description: Returns true if the map has a value associated with
28 // the given index, false otherwise.
29 ////////////////////////////////////////////////////////////////////
30 bool LwoVertexMap::
31 has_value(int index) const {
32  return (_vmap.count(index) != 0);
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: LwoVertexMap::get_value
37 // Access: Public
38 // Description: Returns the mapping value associated with the given
39 // index, or an empty PTA_stdfloat if there is no mapping
40 // value associated.
41 ////////////////////////////////////////////////////////////////////
42 PTA_stdfloat LwoVertexMap::
43 get_value(int index) const {
44  VMap::const_iterator vi;
45  vi = _vmap.find(index);
46  if (vi != _vmap.end()) {
47  return (*vi).second;
48  }
49 
50  return PTA_stdfloat();
51 }
52 
53 ////////////////////////////////////////////////////////////////////
54 // Function: LwoVertexMap::read_iff
55 // Access: Public, Virtual
56 // Description: Reads the data of the chunk in from the given input
57 // file, if possible. The ID and length of the chunk
58 // have already been read. stop_at is the byte position
59 // of the file to stop at (based on the current position
60 // at in->get_bytes_read()). Returns true on success,
61 // false otherwise.
62 ////////////////////////////////////////////////////////////////////
63 bool LwoVertexMap::
64 read_iff(IffInputFile *in, size_t stop_at) {
65  LwoInputFile *lin = DCAST(LwoInputFile, in);
66 
67  _map_type = lin->get_id();
68  _dimension = lin->get_be_uint16();
69  _name = lin->get_string();
70 
71  while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
72  int index = lin->get_vx();
73 
74  PTA_stdfloat value;
75  for (int i = 0; i < _dimension; i++) {
76  value.push_back(lin->get_be_float32());
77  }
78 
79  bool inserted = _vmap.insert(VMap::value_type(index, value)).second;
80  if (!inserted) {
81  nout << "Duplicate index " << index << " in map.\n";
82  }
83  }
84 
85  return (lin->get_bytes_read() == stop_at);
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: LwoVertexMap::write
90 // Access: Public, Virtual
91 // Description:
92 ////////////////////////////////////////////////////////////////////
93 void LwoVertexMap::
94 write(ostream &out, int indent_level) const {
95  indent(out, indent_level)
96  << get_id() << " { map_type = " << _map_type
97  << ", dimension = " << _dimension
98  << ", name = \"" << _name << "\", "
99  << _vmap.size() << " values }\n";
100 }
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.
A wrapper around an istream used for reading an IFF file.
Definition: iffInputFile.h:33
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.
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: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.