Panda3D

lwoDiscontinuousVertexMap.cxx

00001 // Filename: lwoDiscontinuousVertexMap.cxx
00002 // Created by:  drose (24Apr01)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "lwoDiscontinuousVertexMap.h"
00016 #include "lwoInputFile.h"
00017 
00018 #include "dcast.h"
00019 #include "indent.h"
00020 
00021 #include <algorithm>
00022 
00023 TypeHandle LwoDiscontinuousVertexMap::_type_handle;
00024 
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //     Function: LwoDiscontinuousVertexMap::has_value
00028 //       Access: Public
00029 //  Description: Returns true if the map has a value associated with
00030 //               the given index, false otherwise.
00031 ////////////////////////////////////////////////////////////////////
00032 bool LwoDiscontinuousVertexMap::
00033 has_value(int polygon_index, int vertex_index) const {
00034   VMad::const_iterator di;
00035   di = _vmad.find(polygon_index);
00036   if (di != _vmad.end()) {
00037     const VMap &vmap = (*di).second;
00038     return (vmap.count(vertex_index) != 0);
00039   }
00040 
00041   return false;
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: LwoDiscontinuousVertexMap::get_value
00046 //       Access: Public
00047 //  Description: Returns the mapping value associated with the given
00048 //               index, or an empty PTA_stdfloat if there is no mapping
00049 //               value associated.
00050 ////////////////////////////////////////////////////////////////////
00051 PTA_stdfloat LwoDiscontinuousVertexMap::
00052 get_value(int polygon_index, int vertex_index) const {
00053   VMad::const_iterator di;
00054   di = _vmad.find(polygon_index);
00055   if (di != _vmad.end()) {
00056     const VMap &vmap = (*di).second;
00057     VMap::const_iterator vi;
00058     vi = vmap.find(vertex_index);
00059     if (vi != vmap.end()) {
00060       return (*vi).second;
00061     }
00062   }
00063 
00064   return PTA_stdfloat();
00065 }
00066 
00067 ////////////////////////////////////////////////////////////////////
00068 //     Function: LwoDiscontinuousVertexMap::read_iff
00069 //       Access: Public, Virtual
00070 //  Description: Reads the data of the chunk in from the given input
00071 //               file, if possible.  The ID and length of the chunk
00072 //               have already been read.  stop_at is the byte position
00073 //               of the file to stop at (based on the current position
00074 //               at in->get_bytes_read()).  Returns true on success,
00075 //               false otherwise.
00076 ////////////////////////////////////////////////////////////////////
00077 bool LwoDiscontinuousVertexMap::
00078 read_iff(IffInputFile *in, size_t stop_at) {
00079   LwoInputFile *lin = DCAST(LwoInputFile, in);
00080 
00081   _map_type = lin->get_id();
00082   _dimension = lin->get_be_uint16();
00083   _name = lin->get_string();
00084 
00085   while (lin->get_bytes_read() < stop_at && !lin->is_eof()) {
00086     int vertex_index = lin->get_vx();
00087     int polygon_index = lin->get_vx();
00088 
00089     PTA_stdfloat value;
00090     for (int i = 0; i < _dimension; i++) {
00091       value.push_back(lin->get_be_float32());
00092     }
00093 
00094     VMap &vmap = _vmad[polygon_index];
00095     pair<VMap::iterator, bool> ir =
00096       vmap.insert(VMap::value_type(vertex_index, value));
00097     if (!ir.second) {
00098       // This polygon/vertex pair was repeated in the vmad.  Is it
00099       // simply redundant, or is it contradictory?
00100       PTA_stdfloat orig_value = (*ir.first).second;
00101 
00102       if (value.v() != orig_value.v()) {
00103         nout << "Multiple UV values for vertex " << vertex_index
00104              << " of polygon " << polygon_index
00105              << " specified by discontinuous vertex map.\n"
00106              << "Original value = ";
00107 
00108         PTA_stdfloat::const_iterator vi;
00109         for (vi = orig_value.begin(); vi != orig_value.end(); ++vi) {
00110           nout << (*vi) << " ";
00111         }
00112         nout << " new value = ";
00113         for (vi = value.begin(); vi != value.end(); ++vi) {
00114           nout << (*vi) << " ";
00115         }
00116         nout << "\n";
00117       }
00118     }
00119   }
00120 
00121   return (lin->get_bytes_read() == stop_at);
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: LwoDiscontinuousVertexMap::write
00126 //       Access: Public, Virtual
00127 //  Description:
00128 ////////////////////////////////////////////////////////////////////
00129 void LwoDiscontinuousVertexMap::
00130 write(ostream &out, int indent_level) const {
00131   indent(out, indent_level)
00132     << get_id() << " { map_type = " << _map_type
00133     << ", dimension = " << _dimension
00134     << ", name = \"" << _name << "\", "
00135     << _vmad.size() << " polygons }\n";
00136 }
 All Classes Functions Variables Enumerations