Panda3D
lwoPolygonTags.cxx
1 // Filename: lwoPolygonTags.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 "lwoPolygonTags.h"
16 #include "lwoInputFile.h"
17 
18 #include "dcast.h"
19 #include "indent.h"
20 
21 TypeHandle LwoPolygonTags::_type_handle;
22 
23 
24 ////////////////////////////////////////////////////////////////////
25 // Function: LwoPolygonTags::has_tag
26 // Access: Public
27 // Description: Returns true if the map has a tag associated with
28 // the given polygon index, false otherwise.
29 ////////////////////////////////////////////////////////////////////
31 has_tag(int polygon_index) const {
32  return (_tmap.count(polygon_index) != 0);
33 }
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: LwoPolygonTags::get_tag
37 // Access: Public
38 // Description: Returns the tag associated with the given polygon
39 // index, or -1 if there is no tag associated.
40 ////////////////////////////////////////////////////////////////////
42 get_tag(int polygon_index) const {
43  TMap::const_iterator ti;
44  ti = _tmap.find(polygon_index);
45  if (ti != _tmap.end()) {
46  return (*ti).second;
47  }
48 
49  return -1;
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: LwoPolygonTags::read_iff
54 // Access: Public, Virtual
55 // Description: Reads the data of the chunk in from the given input
56 // file, if possible. The ID and length of the chunk
57 // have already been read. stop_at is the byte position
58 // of the file to stop at (based on the current position
59 // at in->get_bytes_read()). Returns true on success,
60 // false otherwise.
61 ////////////////////////////////////////////////////////////////////
63 read_iff(IffInputFile *in, size_t stop_at) {
64  LwoInputFile *lin = DCAST(LwoInputFile, in);
65 
66  _tag_type = lin->get_id();
67 
68  while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
69  int polygon_index = lin->get_vx();
70  int tag = lin->get_be_int16();
71 
72  bool inserted = _tmap.insert(TMap::value_type(polygon_index, tag)).second;
73  if (!inserted) {
74  nout << "Duplicate index " << polygon_index << " in map.\n";
75  }
76  }
77 
78  return (lin->get_bytes_read() == stop_at);
79 }
80 
81 ////////////////////////////////////////////////////////////////////
82 // Function: LwoPolygonTags::write
83 // Access: Public, Virtual
84 // Description:
85 ////////////////////////////////////////////////////////////////////
86 void LwoPolygonTags::
87 write(ostream &out, int indent_level) const {
88  indent(out, indent_level)
89  << get_id() << " { tag_type = " << _tag_type << ", "
90  << _tmap.size() << " values }\n";
91 }
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:56
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_int16 get_be_int16()
Extracts a signed 16-bit big-endian integer.
int get_tag(int polygon_index) const
Returns the tag associated with the given polygon index, or -1 if there is no tag associated...
A wrapper around an istream used for reading an IFF file.
Definition: iffInputFile.h:33
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
bool has_tag(int polygon_index) const
Returns true if the map has a tag associated with the given polygon index, false otherwise.
IffId get_id()
Extracts a 4-character IFF ID.