Ejemplo n.º 1
0
 public function parse()
 {
     if (!file_exists($this->ops_ply)) {
         return false;
     }
     $vn_mode = PlyToStl::MODE_HEADER;
     $vo_handle = @fopen($this->ops_ply, "r");
     $vn_v = $vn_f = 0;
     if ($vo_handle) {
         if (($vs_line = fgets($vo_handle)) !== false) {
             // make sure we're dealing with a ply file
             if (!PlyToStl::startswith($vs_line, 'ply')) {
                 return false;
             }
         }
         while (($vs_line = fgets($vo_handle)) !== false) {
             switch ($vn_mode) {
                 case PlyToStl::MODE_HEADER:
                     // process headers (face and element count)
                     if (PlyToStl::startswith($vs_line, 'element face')) {
                         $this->opn_element_face = intval(PlyToStl::getword($vs_line, 2));
                         $this->opa_faces = new SplFixedArray($this->opn_element_face);
                     }
                     if (PlyToStl::startswith($vs_line, 'element vertex')) {
                         $this->opn_element_vertex = intval(PlyToStl::getword($vs_line, 2));
                         $this->opa_vertices = new SplFixedArray($this->opn_element_vertex);
                     }
                     // end of header reached, switch to vertex mode (they're next in the file)
                     if (PlyToStl::startswith($vs_line, 'end_header')) {
                         $vn_mode = PlyToStl::MODE_VERTEX;
                     }
                     break;
                 case PlyToStl::MODE_VERTEX:
                     // instead of simply adding a (memory-consuming) PHP array here, we pack
                     // the values we need in a binary string. this is much more memory-efficient,
                     // especially in combination with using SplFixedArray
                     $this->opa_vertices[$vn_v] = pack("f*", floatval(PlyToStl::getword($vs_line, 0)), floatval(PlyToStl::getword($vs_line, 1)), floatval(PlyToStl::getword($vs_line, 2)));
                     $vn_v++;
                     // vertex count reached, switch to faces
                     if ($vn_v == $this->opn_element_vertex) {
                         $vn_mode = PlyToStl::MODE_FACES;
                     }
                     break;
                 case PlyToStl::MODE_FACES:
                     // instead of simply adding a (memory-consuming) PHP array here, we pack
                     // the values we need in a binary string. this is much more memory-efficient,
                     // especially in combination with using SplFixedArray
                     $this->opa_faces[$vn_f] = pack("I*", intval(PlyToStl::getword($vs_line, 1)), intval(PlyToStl::getword($vs_line, 2)), intval(PlyToStl::getword($vs_line, 3)));
                     $vn_f++;
                     // face count reached, we're done
                     if ($vn_f == $this->opn_element_face) {
                         $vn_mode = PlyToStl::MODE_NONE;
                         break 2;
                     }
                     break;
                 default:
                     break;
                     // shouldn't happen
             }
         }
         fclose($vo_handle);
     }
 }