OBJIndex OBJModel::ParseOBJIndex(const std::string& token, bool* hasUVs, bool* hasNormals)
{
    unsigned int tokenLength = token.length();
    const char* tokenString = token.c_str();
    
    unsigned int vertIndexStart = 0;
    unsigned int vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, '/');
    
    OBJIndex result;
    result.vertexIndex = ParseOBJIndexValue(token, vertIndexStart, vertIndexEnd);
    result.uvIndex = 0;
    result.normalIndex = 0;
    
    if(vertIndexEnd >= tokenLength)
        return result;
    
    vertIndexStart = vertIndexEnd + 1;
    vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, '/');
    
    result.uvIndex = ParseOBJIndexValue(token, vertIndexStart, vertIndexEnd);
    *hasUVs = true;
    
    if(vertIndexEnd >= tokenLength)
        return result;
    
    vertIndexStart = vertIndexEnd + 1;
    vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, '/');
    
    result.normalIndex = ParseOBJIndexValue(token, vertIndexStart, vertIndexEnd);
    *hasNormals = true;
    
    return result;
}
glm::vec2 OBJModel::ParseOBJVec2(const std::string& line)
{
    unsigned int tokenLength = line.length();
    const char* tokenString = line.c_str();
    
    unsigned int vertIndexStart = 3;
    
    while(vertIndexStart < tokenLength)
    {
        if(tokenString[vertIndexStart] != ' ')
            break;
        vertIndexStart++;
    }
    
    unsigned int vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
    
    float x = ParseOBJFloatValue(line, vertIndexStart, vertIndexEnd);
    
    vertIndexStart = vertIndexEnd + 1;
    vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');
    
    float y = ParseOBJFloatValue(line, vertIndexStart, vertIndexEnd);
    
    return glm::vec2(x,y);
}
Esempio n. 3
0
glm::vec3 OBJModel::ParseOBJVec3(const std::string& line)
{
  unsigned int tokenLength = static_cast<unsigned int>(line.length());
  const char* tokenString = line.c_str();

  unsigned int vertIndexStart = 2;

  while (vertIndexStart < tokenLength)
  {
    if (tokenString[vertIndexStart] != ' ')
      break;
    vertIndexStart++;
  }

  unsigned int vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');

  float x = ParseOBJFloatValue(line, vertIndexStart, vertIndexEnd);

  vertIndexStart = vertIndexEnd + 1;
  vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');

  float y = ParseOBJFloatValue(line, vertIndexStart, vertIndexEnd);

  vertIndexStart = vertIndexEnd + 1;
  vertIndexEnd = FindNextChar(vertIndexStart, tokenString, tokenLength, ' ');

  float z = ParseOBJFloatValue(line, vertIndexStart, vertIndexEnd);

  return glm::vec3(x, y, z);

  //glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()))
}
gstStatus gstTXTTable::BuildIndex() {
  const size_t buffsize = 8192;

  // scan entire file to count new lines
  ssize_t bytes_read = 0;
  char buf[buffsize];

  if (::lseek64(file_descriptor_, 0, SEEK_SET) == -1)
    return (status_ = GST_READ_FAIL);

  off64_t block_start = 0;
  off64_t line_start = 0;

  while ((bytes_read = ::read(file_descriptor_, static_cast<void*>(buf),
                              buffsize)) > 0) {
    char* pos = buf;
    while ((pos = FindNextChar('\n', pos, buf + bytes_read)) != NULL) {
      if (skip_rows_ == 0) {
        // skip any totally blank line
        uint size = record_index_.size();
        if (size && record_index_[size - 1] == line_start - 1) {
          record_index_[size - 1] = line_start;
        } else {
          record_index_.push_back(line_start);
        }
      } else {
        --skip_rows_;
      }
      ++pos;
      line_start = block_start + (pos - buf);
    }
    block_start += bytes_read;
  }
  num_rows_ = record_index_.size();

  // sanity check...
  if (record_index_.size() == 0) {
    num_columns_ = 0;
    return (status_ = GST_READ_FAIL);
  }

  notify(NFY_DEBUG, "Indexed %llu rows of file %s",
         static_cast<long long unsigned>(record_index_.size()), name());

  if (read_mode_ == GST_READONLY)
    return (status_ = GST_OKAY);

  //
  // save out index file, if possible
  //
  std::string index_file = khReplaceExtension(name(), kIndexExtension);
  int indexfd;
  if ((indexfd = ::open(index_file.c_str(), O_CREAT | O_TRUNC | O_WRONLY | O_NONBLOCK,
                        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) {
    notify(NFY_WARN, "Unable to write index file %s", index_file.c_str());
    // this is not a fatal error.  we still have the index in memory
    return (status_ = GST_OKAY);
  }

  CloseFileCatcher fdcatch(indexfd);

  uint64 size;
  time_t mtime;
  if (!khGetFileInfo(name(), size, mtime)) {
    return (status_ = GST_WRITE_FAIL);
  }

  IndexHeader hdr;
  memcpy(hdr.magic, TXT_INDEX_MAGIC, 8);
  hdr.numrows = record_index_.size();
  hdr.numcols = 0;    // currently unused
  hdr.mtime = mtime;
  hdr.offset = sizeof(hdr);

  if (::write(indexfd, &hdr, sizeof(IndexHeader)) != sizeof(IndexHeader)) {
    notify(NFY_WARN, "Unable to write header to txt index file %s",
           index_file.c_str());
    return (status_ = GST_WRITE_FAIL);
  }

  if (::write(indexfd, &record_index_[0], record_index_.size() * sizeof(off64_t)) !=
      ssize_t(record_index_.size() * sizeof(off64_t))) {
    notify(NFY_WARN, "Unable to write txt index file %s", index_file.c_str());
    return (status_ = GST_WRITE_FAIL);
  }

  return (status_ = GST_OKAY);
}