![](/img/trans.png)
[英]Should I always use a 1D vector with my own indexing, or is a multi-dimensional vector ok?
[英]Improper indexing of 3 dimensional vector
這是我的功能。 基本上,它讀取文本文件並將數據組織成一堆不同的向量。
ctrlpts
是3維向量。 第一層存儲不同組的控制點(cp1..cp3)。 第二層存儲x,y,z,w坐標數組。 因此,第三級代表坐標數組中的條目。
在下面的函數中,我首先初始化所有向量。 然后,根據維數( int d
)和結矢量( int k
)的數量,我相應地構建多維矢量。 對於ctrlpts
我首先將適當的坐標矢量添加到適當的控制點集中。 (例如: cpl.push_back(x1)
)然后將這些坐標集推入ctrlpts
(例如: ctrlpts.push_back(cp1)
)
當我調用ctrlpts[0][0][0]
時出現錯誤。
void createNurb(string filename){
ifstream file;
file.open(filename.c_str());
if(file.fail()){
cout << "Cannot open " << filename << endl;
}
vector<float> knots1;
vector<float> knots2;
vector<float> knots3;
vector< vector<float> > knotvectors;
vector<float> x1;
vector<float> y1;
vector<float> z1;
vector<float> w1;
vector<float> x2;
vector<float> y2;
vector<float> z2;
vector<float> w2;
vector<float> x3;
vector<float> y3;
vector<float> z3;
vector<float> w3;
vector< vector<float> > cp1;
vector< vector<float> > cp2;
vector< vector<float> > cp3;
vector< vector< vector<float> > > ctrlpts;
string line;
getline(file,line);
int d = line[0] - 48;
getline(file,line);
int k= line[0] - 48;
if(k==1){
knotvectors.push_back(knots1);
if(d==1){
cp1.push_back(x1);
cp1.push_back(w1);
}else if(d==2){
cp1.push_back(x1);
cp1.push_back(y1);
cp1.push_back(w1);
}else{
cp1.push_back(x1);
cp1.push_back(y1);
cp1.push_back(z1);
cp1.push_back(w1);
}
ctrlpts.push_back(cp1);
}else if(k==2){
knotvectors.push_back(knots1);
knotvectors.push_back(knots2);
if(d==1){
cp1.push_back(x1);
cp1.push_back(w1);
cp2.push_back(x2);
cp2.push_back(w2);
}else if(d==2){
cp1.push_back(x1);
cp1.push_back(y1);
cp1.push_back(w1);
cp2.push_back(x2);
cp2.push_back(y2);
cp2.push_back(w2);
}else{
cp1.push_back(x1);
cp1.push_back(y1);
cp1.push_back(z1);
cp1.push_back(w1);
cp2.push_back(x2);
cp2.push_back(y2);
cp2.push_back(z2);
cp2.push_back(w2);
}
ctrlpts.push_back(cp1);
ctrlpts.push_back(cp2);
}else{
knotvectors.push_back(knots1);
knotvectors.push_back(knots2);
knotvectors.push_back(knots3);
if(d==1){
cp1.push_back(x1);
cp1.push_back(w1);
cp2.push_back(x2);
cp2.push_back(w2);
cp2.push_back(x3);
cp2.push_back(w3);
}else if(d==2){
cp1.push_back(x1);
cp1.push_back(y1);
cp1.push_back(w1);
cp2.push_back(x2);
cp2.push_back(y2);
cp2.push_back(w2);
cp3.push_back(x3);
cp3.push_back(y3);
cp3.push_back(w3);
}else{
cp1.push_back(x1);
cp1.push_back(y1);
cp1.push_back(z1);
cp1.push_back(w1);
cp2.push_back(x2);
cp2.push_back(y2);
cp2.push_back(z2);
cp2.push_back(w2);
cp3.push_back(x3);
cp3.push_back(y3);
cp3.push_back(z3);
cp3.push_back(w3);
}
ctrlpts.push_back(cp1);
ctrlpts.push_back(cp2);
ctrlpts.push_back(cp3);
}
for(int i=0; i<k; i++){
getline(file,line);
std::istringstream iss(line);
std::copy(std::istream_iterator<float>(iss),
std::istream_iterator<float>(),
std::back_inserter(knotvectors[i]));
for (int j=0; j<=d; j++){
getline(file,line);
std::copy(std::istream_iterator<float>(iss),
std::istream_iterator<float>(),
std::back_inserter(ctrlpts[i][j]));
}
}
cout<< ctrlpts[0][0][0] << endl;
file.close();
}
如果我調用knotvectors[0][0]
,一切都會正常,所以我不確定在引用3維ctrlpts
向量的單個單元格時我做錯了什么。
關於我在做什么錯的任何想法嗎? 也許與我組裝此3維矢量的方式有關?
編輯:沒有特定的錯誤信息。 它只是崩潰。
編輯2:
我相信問題可能在這里:
for (int j=0; j<=d; j++){
getline(file,line);
std::copy(std::istream_iterator<float>(iss),
std::istream_iterator<float>(),
std::back_inserter(ctrlpts[i][j]));
如果我打印出來: ctrlpts[0][0].size()
則說為0。因此,我認為上面的塊由於某種原因沒有將數據輸入向量中。
找出問題所在。
如果具有向量A,B和C。如果將B推入A,然后將C推入B,則不能直接將數字推入C並通過A來訪問它們。相反,所有事情都必須通過A處理:
因此,當我最初將控制點集(將cpi
轉換為ctrlpts
然后將其更改時,問題已解決:
cp1.push_back(x1);
至
ctrlpts[i].push_back(xi);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.