Depois de brigar um pouco com o c++ consegui "traduzir" mais um exemplo de procedural
primitives que vi em python para a linguagem c++ , dessa vez usando um exemplo de como gerar esferas dispostas aleatoriamente ao redor de uma esfera maior .
/*
* RibBox.h
* classExerc
*
* Created by chrysl666 on 8/27/11.
* Copyright 2011 none. All rights reserved.
*
*/
#ifndef RIBBOX_H
#define RIBBOX_H
#include
#include
int const VECTOR_SIZE = 3;
class RibBox
{
public:
//constructor
RibBox();
//variaveis
std::string parFromMaya ;// sent by renderman
std::string details ;// got from splited[0]
std::string number ;// got from splited[1]
std::string width ;// got from splited[2]
std::string radius ;// got from splited[3]
double point[VECTOR_SIZE];// ponto aleatorio
double normPoint[VECTOR_SIZE];// ponto apos normalizado
double scalePnt[VECTOR_SIZE];// ponto apos scalado
//funcoes
void setRibBox(); // insert commands at render stream
void getRibBoxPar(); // get par from renderman
void Tokenize(const std::string& ,
std::vector
const std::string& ); //tokenize
void randomPoint(double rangeMin, double rangeMax );
std::string formatTuple(std::string theTuple);
void scalePoint();
void normalize();
double randWidth(std::string max);
};
#endif
cpp:
/*
* RibBox.cpp
* classExerc
*
* Created by chrysl666 on 8/27/11.
* Copyright 2011 none. All rights reserved.
*
*/
#include
#include
#include
#include
#include
#include "RibBox.h"
RibBox::RibBox()
{
// construtor da classe
//incializa o array point
for(int i=0;i<3;i++)
{
point[i]=0;
}
}
void RibBox::Tokenize(const std::string& str,
std::vector
const std::string& delimiters = " ")
{
// tokenize function para separar as informacoes em strings
// pula o delimitadores no inicio
std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// acha o primeiro nao delimitador
std::string::size_type pos = str.find_first_of(delimiters, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
{
// achou um token , add no vetor.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// pula delimitadores. Note o "nao_de"
lastPos = str.find_first_not_of(delimiters, pos);
// procura proximo "nao-delimitador"
pos = str.find_first_of(delimiters, lastPos);
}
}
void RibBox::getRibBoxPar()
{
//parsing comandos vindos dos renderman
std::vector
std::getline(std::cin,parFromMaya);
Tokenize(parFromMaya ,tokens);
details = tokens[0];
number = tokens[1];
width = tokens[2];
radius = tokens[3];
}
void RibBox::setRibBox()
{
//formatacao dos comandos de saida para o stream
std::cout<<"AttributeBegin\n";
std::cout<<"Points \"P\" [";
for (int i=0; i< (atoi(number.c_str())); i++)
{
randomPoint(-1, 1);
normalize();
scalePoint();
std::cout<
}
std::cout<<"]\n";
std::cout<<"\"width\" [";
for (int i=0; i< (atoi(number.c_str())); i++)
{
std::cout<<randWidth(width)<<" ";
}
std::cout<<"] \n";
std::cout<<"\"Cs\" [";
for (int i=0; i< (atoi(number.c_str())); i++)
{
std::cout<<randWidth("1")<<" "<<randWidth("1")<<" "<<randWidth("1")<<" " ;
}
std::cout<<"]\n";
std::cout<<"AttributeEnd\n";
std::cout<<"\377"<
}
void RibBox::randomPoint(double rangeMin, double rangeMax )
{
//cria um valor aleatorio entre rangeMin e rangeMax para o arrayPoint
double step = rangeMax - rangeMin ;
for (int i=0; i<VECTOR_SIZE; i++)
{
point[i] = (step * static_cast <double> (rand ()) / static_cast <double> (RAND_MAX) -1);
}
}
void RibBox::normalize()
{
double mag = sqrt(point[0] * point[0] + point[1] * point[1] + point[2] * point[2]);
for (int i=0; i<VECTOR_SIZE; i++)
{
normPoint[i] = point[i]/mag;
}
}
void RibBox::scalePoint()
{
int radiusInt = atoi(radius.c_str());
for (int i=0; i<VECTOR_SIZE; i++)
{
scalePnt[i] = normPoint[i] * radiusInt;
}
}
double RibBox::randWidth(std::string max)
{
double maxInt = atof(max.c_str());
return (maxInt * static_cast <double> (rand ()) / static_cast <double> (RAND_MAX));
}
e o main.cpp :
#include
#include
#include
#include
#include "RibBox.h"
int main (int argc, char * const argv[])
{
RibBox ribBoxObj;
while(1)
{
ribBoxObj.getRibBoxPar();
ribBoxObj.setRibBox();
}
}
e o comando de insercao no maya :
RiProcedural -pn "/Users/chrysl666/Desktop/devBook/classExerc02/build/Debug/classExerc02" -p "1000000 0.05 10" -b -1e38 1e38 -1e38 1e38 -1e38 1e38;