首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否删除Vector中的重复项?

是否删除Vector中的重复项?
EN

Stack Overflow用户
提问于 2011-11-24 23:34:15
回答 3查看 3K关注 0票数 1

我有一个定义如下的顶点:

代码语言:javascript
复制
struct VERTEX
{
  XMFLOAT3 Pos  
  XMFLOAT2 UV    

}; 

我像这样定义了索引,纹理协调索引,UVcoordinate和顶点

代码语言:javascript
复制
vector <int> indices ;
vector <int> Texcoordindex ; 
vector <XMFLOAT2> UVCoinate ;
vector <XMFLOAT3> Verices;

好的。我从一个3D文件(本例中是一个obj文件)设置了索引、纹理协调索引、UVcoordinate和顶点。

现在,当我想定义一个面数组时,我发现一些顶点是用多个UV坐标定义的。我想移除具有相同UV坐标的重复顶点,并创建新的索引和顶点向量,以便仅存储具有顶点新索引的非重复顶点。

下面是obj文件:

代码语言:javascript
复制
# This file uses centimeters as units for non-parametric coordinates.

v -12.830015 0.000000 12.061520
v 12.027525 0.000000 12.061520
v -12.830015 15.862570 12.061520
v 12.027525 15.862570 12.061520
v -12.830015 15.862570 -12.622024
v 12.027525 15.862570 -12.622024
v -12.830015 0.000000 -12.622024
v 12.027525 0.000000 -12.622024

vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000
f 1/1 2/2 3/3
f 3/3 2/2 4/4
f 3/3 4/4 5/5
f 5/5 4/4 6/6
f 5/5 6/6 7/7
f 7/7 6/6 8/8
f 7/7 8/8 1/9
f 1/9 8/8 2/10
f 2/2 8/11 4/4
f 4/4 8/11 6/12
f 7/13 1/1 5/14
f 5/14 1/1 3/3

我希望结果是这样的

代码语言:javascript
复制
# This file uses centimeters as units for non-parametric coordinates.

v -12.830015 0.000000 12.061520
v 12.027525 0.000000 12.061520
v -12.830015 15.862570 12.061520
v 12.027525 15.862570 12.061520
v -12.830015 15.862570 -12.622024
v 12.027525 15.862570 -12.622024
v -12.830015 0.000000 -12.622024
v 12.027525 0.000000 -12.622024

vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000

删除重复后的新折点

代码语言:javascript
复制
f 1/1 2/2 3/3
f 4/4 5/5 6/6 
f 7/7 8/8 1/9 
f 2/10 8/116/12
f7/13 5/14

我尝试使用sort和unique,但遇到编译错误:

代码语言:javascript
复制
error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'VERTEX'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'VERTEX' c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'VERTEX'   c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const 
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'VERTEX'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'VERTEX'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2676: binary '<' : 'VERTEX' does not define this operator or a conversion to a type acceptable to the predefined operator    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C1903: unable to recover from previous error(s); stopping compilation c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-25 00:20:29

如果没有任何类型的内存使用限制,您可以声明一个新的向量保留大小与您的相同,并使用std::find函数只输入不在此向量中的值,以确定值是否已经在新的向量中。

代码语言:javascript
复制
std::vector newVector;
newVector.reserve( oldVector.size() );

对于旧向量的所有元素:

代码语言:javascript
复制
if( newVector.find(element))
{
  if( find( newVector.begin(), newVector.end(), newVector.size() != newVector.end() )
  {
    newVector.push_back(element);
   }
}

其他算法可以是在重复的情况下将向量中的所有元素移回预览位置(此方法仅适用于排序的数组列表)。

票数 2
EN

Stack Overflow用户

发布于 2011-11-24 23:48:27

没有代码和编译器错误,就不可能诊断问题。但使用std::sortstd::uniqueindices向量中删除重复项的示例如下:

代码语言:javascript
复制
std::sort(indices.begin(), indices.end());
vector<int>::iterator it = std::unique(indices.begin(), indices.end());
indices.erase(it,indices.end());

我不知道XMLFLOAT3XMLFLOAT2类型是什么,所以可能有必要向std::sortstd::unique传递一个谓词。

编辑:对于XMFLOAT3

代码语言:javascript
复制
bool xmfloat3_lt(const XMFLOAT3 f1, const XMFLOAT3 f2)
{
    if (f1.x < f2.x) return true;
    if (f1.y < f2.y) return true;
    if (f1.z < f2.z) return true;
    return false;
}

bool xmfloat3_eq(const XMFLOAT3 f1, const XMFLOAT3 f2)
{
    return f1.x == f2.x &&
           f1.y == f2.y &&
           f1.z == f2.z;
}

std::sort(Verices.begin(), Verices.end(), xmfloat3_lt);
vector<XMFLOAT3>::iterator it = std::unique(
    Verices.begin(), Verices.end(), xmfloat3_eq);
Verices.erase(it,Verices.end());

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2011-11-25 00:26:14

我知道编译器的输出是令人困惑的,但它试图告诉你的是,它不知道如何比较两个VERTEX对象。你可以在你的类中定义一个operator<:

代码语言:javascript
复制
struct VERTEX
{
  XMFLOAT3 Pos;  
  XMFLOAT2 UV;

  bool operator<(const VERTEX &that) const
  {
    // return true if you consider this vertex to be less than that vertex.
  }
};

或作为全局函数

代码语言:javascript
复制
bool operator<(const VERTEX &v1,const VERTEX &v2)
{
  // return true if you consider v1 to be less than v2
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8259430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档