Panda3D
fltVertexList.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 fltVertexList.cxx
10  * @author drose
11  * @date 2000-08-25
12  */
13 
14 #include "fltVertexList.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 #include "fltHeader.h"
18 
19 TypeHandle FltVertexList::_type_handle;
20 
21 /**
22  *
23  */
24 FltVertexList::
25 FltVertexList(FltHeader *header) : FltRecord(header) {
26 }
27 
28 /**
29  * Returns the number of vertices in this vertex list.
30  */
32 get_num_vertices() const {
33  return _vertices.size();
34 }
35 
36 /**
37  * Returns the nth vertex of this vertex list.
38  */
40 get_vertex(int n) const {
41  nassertr(n >= 0 && n < (int)_vertices.size(), nullptr);
42  return _vertices[n];
43 }
44 
45 /**
46  * Removes all vertices from this vertex list.
47  */
50  _vertices.clear();
51 }
52 
53 /**
54  * Adds a new vertex to the end of the vertex list.
55  */
57 add_vertex(FltVertex *vertex) {
58  _header->add_vertex(vertex);
59  _vertices.push_back(vertex);
60 }
61 
62 /**
63  * Writes a quick one-line description of the record, but not its children.
64  * This is a human-readable description, primarily for debugging; to write a
65  * flt file, use FltHeader::write_flt().
66  */
68 output(std::ostream &out) const {
69  out << _vertices.size() << " vertices";
70 }
71 
72 /**
73  * Fills in the information in this bead based on the information given in the
74  * indicated datagram, whose opcode has already been read. Returns true on
75  * success, false if the datagram is invalid.
76  */
77 bool FltVertexList::
78 extract_record(FltRecordReader &reader) {
79  if (!FltRecord::extract_record(reader)) {
80  return false;
81  }
82 
83  nassertr(reader.get_opcode() == FO_vertex_list, false);
84  DatagramIterator &iterator = reader.get_iterator();
85 
86  _vertices.clear();
87  while (iterator.get_remaining_size() >= 4) {
88  int vertex_offset = iterator.get_be_int32();
89  _vertices.push_back(_header->get_vertex_by_offset(vertex_offset));
90  }
91 
92  check_remaining_size(iterator);
93  return true;
94 }
95 
96 /**
97  * Fills up the current record on the FltRecordWriter with data for this
98  * record, but does not advance the writer. Returns true on success, false if
99  * there is some error.
100  */
101 bool FltVertexList::
102 build_record(FltRecordWriter &writer) const {
103  if (!FltRecord::build_record(writer)) {
104  return false;
105  }
106 
107  writer.set_opcode(FO_vertex_list);
108  Datagram &datagram = writer.update_datagram();
109 
110  Vertices::const_iterator vi;
111  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
112  datagram.add_be_uint32(_header->get_offset_by_vertex(*vi));
113  }
114 
115  return true;
116 }
A class to retrieve the individual data elements previously stored in a Datagram.
int32_t get_be_int32()
Extracts a signed 32-bit big-endian integer.
size_t get_remaining_size() const
Return the bytes left in the datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:181
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
The base class for all kinds of records in a MultiGen OpenFlight file.
Definition: fltRecord.h:36
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
Definition: fltRecord.cxx:254
void add_vertex(FltVertex *vertex)
Adds a new vertex to the end of the vertex list.
FltVertex * get_vertex(int n) const
Returns the nth vertex of this vertex list.
void clear_vertices()
Removes all vertices from this vertex list.
int get_num_vertices() const
Returns the number of vertices in this vertex list.
virtual void output(std::ostream &out) const
Writes a quick one-line description of the record, but not its children.
Represents a single vertex in the vertex palette.
Definition: fltVertex.h:32
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.