home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1997 March
/
VPR9703A.ISO
/
VPR_DATA
/
DOGA
/
SOURCES
/
POLYEDIT.LZH
/
MODEL
/
VSETLIB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1996-07-25
|
11KB
|
417 lines
/*
* 頂点集合
*
* Copyright T.Kobayashi 1994.7.9
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "matrix.h"
#include "vector.h"
#include "matclass.h"
#include "strclass.h"
#include "ml.h"
#include "poly.h"
#include "view.h"
int VertexSetClassID ;
static int VertexClassID;
static int MatrixClassID;
static int FuncVertexSet(int ident, int args, DataStruct *buf);
static int FuncToString(int ident, int args, DataStruct *buf);
static int FuncVertex2Select(int ident, int args, DataStruct *buf);
static int FuncSelect2Vertex(int ident, int args, DataStruct *buf);
static int FuncVertexs(int ident, int args, DataStruct *buf);
static int FuncVertexGetVertex(int ident, int args, DataStruct *buf);
static int FuncDrawVertex(int ident, int args, DataStruct *buf);
static int FuncVertexEqual(int ident, int args, DataStruct *buf);
static int FuncVertexLogical(int ident, int args, DataStruct *buf);
static int FuncVertexMulMatrix(int ident, int args, DataStruct *buf);
static int FuncVertexAddVertex(int ident, int args, DataStruct *buf);
static int FuncVertexPositionVertex(int ident, int args, DataStruct *buf);
static int FuncVertexSelectPosition(int ident, int args, DataStruct *buf);
void VertexSetLibInit()
{
VertexSetClassID = NewClass( "VertexSet", 0 );
VertexClassID = ClassName("Vertex");
MatrixClassID = ClassName("Matrix");
NewFunction( 0, "VertexSet", FuncVertexSet );
NewFunction( 0, "VertexSelect", FuncSelect2Vertex );
NewFunction( VertexSetClassID, "tostring", FuncToString );
NewFunction( VertexSetClassID, "VertexSelect", FuncVertex2Select );
NewFunction( VertexSetClassID, "VertexGetVertex", FuncVertexGetVertex );
NewFunction( VertexSetClassID, "Vertexs", FuncVertexs );
NewFunction( VertexSetClassID, "DrawVertex", FuncDrawVertex );
NewFunction( VertexSetClassID, "VertexPositionVertex", FuncVertexPositionVertex );
NewFunction( VertexSetClassID, "VertexSelectPosition", FuncVertexSelectPosition );
NewOperator( VertexSetClassID, OPE_EQ, FuncVertexEqual );
NewOperator( VertexSetClassID, OPE_NOTEQ, FuncVertexEqual );
NewOperator( VertexSetClassID, OPE_AND, FuncVertexLogical );
NewOperator( VertexSetClassID, OPE_OR, FuncVertexLogical );
NewOperator( VertexSetClassID, OPE_XOR, FuncVertexLogical );
NewOperator( VertexSetClassID, OPE_MULT, FuncVertexLogical );
NewOperator( VertexSetClassID, OPE_PLUS, FuncVertexLogical );
NewOperator( VertexSetClassID, OPE_MINUS, FuncVertexLogical );
}
/* 頂点集合を生成する */
static int FuncVertexSet( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *verset = NULL;
if (args == 0) {
verset = VertexAlloc(0);
} else if (args == 1 && ObjectCheck( &buf[0], VertexClassID )) {
verset = VertexAppend(VertexAlloc(0),&(((VertexClass*)(buf[0].od.ptr))->ver));
} else if (args == 1 && ObjectCheck( &buf[0], VectorClassID )) {
Vector *vec;
Vertex ver;
memset(&ver, 0, sizeof(Vertex));
vec = &(((VectorClass*)(buf[0].od.ptr))->vec);
ver.x = (short)vec->x;
ver.y = (short)vec->y;
ver.z = (short)vec->z;
verset = VertexAppend(VertexAlloc(0),&ver);
} else {
ExecError("VertexSet:引数の型が異なる\n");
}
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)verset ;
return RETURN_RETURN ;
}
/* 頂点集合を文字列に */
static int FuncToString( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
char *p;
VertexSetClass *verset;
Vertex *ed;
StringClass *str;
int i;
ArgCheck( "tostring", args, buf, TYPE_OBJECT, TYPE_NOASN );
verset = (VertexSetClass*)buf[0].od.ptr;
str = StringAlloc(60 * verset->vers + 1);
p = str->str;
ed = verset->ver;
for (i = 0; i < verset->vers; i++) {
sprintf(p, "(%6d,%6d,%6d) (%4d %4d %4d) (%4d %4d)\n",
ed->x, ed->y, ed->z, ed->vx, ed->vy, ed->vz, ed->u, ed->v);
ed++;
}
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)str ;
return RETURN_RETURN ;
}
/* 頂点集合からポリゴンを選択 */
static int FuncVertex2Select( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "VertexSelect", args, buf, TYPE_OBJECT, TYPE_INT|TYPE_BOOLEAN, TYPE_INT, TYPE_NOASN );
PolySelectVertex( (VertexSetClass*)(buf[0].od.ptr), buf[1].id.i, buf[2].id.i );
return RETURN_VOID ;
}
/* 頂点集合を生成する */
static int FuncSelect2Vertex( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *verset;
if (args > 0) {
ExecError("VertexSelect:引数の型が異なる\n");
}
verset = VertexSelect();
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)verset ;
return RETURN_RETURN ;
}
/* 頂点集合数 */
static int FuncVertexs( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
ArgCheck( "Vertexs", args, buf, TYPE_OBJECT, TYPE_NOASN );
StackPushInt( ((VertexSetClass*)(buf[0].od.ptr))->vers );
return RETURN_RETURN ;
}
/* 頂点取得 */
static int FuncVertexGetVertex( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int pos;
VertexSetClass *verset;
VertexClass *ver ;
verset = (VertexSetClass*)buf[0].od.ptr ;
if (ObjectCheck( &buf[1], VertexClassID )
|| ObjectCheck( &buf[1], VectorClassID ) ) {
Vertex *v, tmpver;
Vector *vec;
ArgCheck( "VertexGetVertex", args, buf, TYPE_OBJECT, TYPE_OBJECT, TYPE_NOASN);
if (ObjectCheck( &buf[1], VertexClassID )) {
v = &(((VertexClass*)buf[1].od.ptr)->ver);
} else {
memset(&tmpver, 0, sizeof(Vertex));
vec = &(((VectorClass*)buf[1].od.ptr)->vec);
tmpver.x = (short)vec->x;
tmpver.y = (short)vec->y;
tmpver.z = (short)vec->z;
v = &tmpver;
}
pos = VertexSearch(verset, v);
StackPushInt(pos);
return RETURN_RETURN;
} else {
ArgCheck( "VertexGetVertex", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_NOASN);
pos = buf[1].id.i;
if (0 <= pos && pos < verset->vers) {
ver = (VertexClass*)ObjectAlloc( sizeof( VertexClass ), VertexClassID );
ver->dtype = POLY_UVSHADE;
ver->ver = verset->ver[pos];
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ver ;
return RETURN_RETURN ;
}
}
return RETURN_VOID ;
}
/* 頂点を描画 */
static int FuncDrawVertex( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
int i;
VertexSetClass *verset;
int color;
if (args == 2) {
ArgCheck( "DrawVertex", args, buf, TYPE_OBJECT, TYPE_INT, TYPE_NOASN );
color = buf[1].id.i;
} else {
ArgCheck( "DrawVertex", args, buf, TYPE_OBJECT, TYPE_NOASN );
color = CURSOR_COLOR;
}
verset = (VertexSetClass*)(buf[0].od.ptr);
for (i = 0; i < verset->vers; i++) {
ViewVertex(&verset->ver[i], color);
}
return RETURN_VOID ;
}
/* 位置のみのVertexSet を得る */
static int FuncVertexPositionVertex( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *ret;
ret = VertexPositionVertex((VertexSetClass*)buf[0].od.ptr);
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN ;
}
/* 指定位置のVertexSet を得る */
static int FuncVertexSelectPosition( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
Vertex v;
VertexSetClass *ret;
if (args == 2 && ObjectCheck( &buf[1], VertexClassID )) {
v = ((VertexClass*)(buf[1].od.ptr))->ver;
} else if (args == 2 && ObjectCheck( &buf[1], VectorClassID )) {
Vector *vec;
vec = &(((VectorClass*)(buf[1].od.ptr))->vec);
v.x = (short)vec->x;
v.y = (short)vec->y;
v.z = (short)vec->z;
} else {
ExecError("VertexSelectPosition: 引数の型が異なる\n");
}
ret = VertexSelectPosition((VertexSetClass*)buf[0].od.ptr, &v);
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN ;
}
/* 頂点集合の等価判定 */
static int FuncVertexEqual( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *verset1, *verset2 ;
if ( args != 2 || ObjectCheck( &buf[1], VertexSetClassID ) == FALSE ) {
ExecError( "論理演算の型が不正です(Vertex)" );
}
verset1 = (VertexSetClass*)buf[0].od.ptr ;
verset2 = (VertexSetClass*)buf[1].od.ptr ;
if (ident == OPE_EQ)
StackPushBoolean(VertexLogicalEqual(verset1, verset2) );
else
StackPushBoolean( ! VertexLogicalEqual(verset1, verset2) );
return RETURN_RETURN ;
}
/* 頂点集合の論理演算 */
static int FuncVertexLogical( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *verset1, *verset2, *verset, *ret ;
if (ident == OPE_MULT && args == 2 && ObjectCheck(&buf[1], MatrixClassID)) {
return FuncVertexMulMatrix(ident, args, buf);
}
if ((ident == OPE_OR || ident == OPE_PLUS || ident == OPE_MINUS)
&& (ObjectCheck(&buf[1], VertexClassID) || ObjectCheck(&buf[1], VectorClassID))) {
return FuncVertexAddVertex(ident, args, buf);
}
if ( args != 2 || ObjectCheck( &buf[1], VertexSetClassID ) == FALSE ) {
ExecError( "論理演算の型が不正です(Vertex)" );
}
verset1 = (VertexSetClass*)buf[0].od.ptr ;
verset2 = (VertexSetClass*)buf[1].od.ptr ;
if (ident == OPE_MINUS || verset1->vers > verset2->vers) {
ret = (VertexSetClass*)ObjectDup((Object*)verset1);
verset = verset2;
} else {
ret = (VertexSetClass*)ObjectDup((Object*)verset2);
verset = verset1;
}
switch( ident ) {
case OPE_AND:
case OPE_MULT:
ret = VertexLogicalAnd(ret, verset);
break;
case OPE_OR:
case OPE_PLUS:
ret = VertexLogicalOr(ret, verset);
break;
case OPE_MINUS:
ret = VertexLogicalSub(ret, verset);
break;
case OPE_XOR:
ret = VertexLogicalXor(ret, verset);
}
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN ;
}
/* 頂点集合の論理演算 */
static int FuncVertexMulMatrix( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *verset, *ret ;
MatrixClass *mat;
if ( args != 2 || ObjectCheck( &buf[1], MatrixClassID ) == FALSE ) {
ExecError( "論理演算の型が不正です(Vertex)" );
}
verset = (VertexSetClass*)buf[0].od.ptr ;
mat = (MatrixClass*)buf[1].od.ptr ;
ret = (VertexSetClass*)ObjectDup((Object*)verset);
ret = VertexMulMatrix(ret, mat);
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN ;
}
/* 頂点集合の論理演算 */
static int FuncVertexAddVertex( ident, args, buf )
int ident ;
int args ;
DataStruct *buf ;
{
VertexSetClass *ret;
Vertex tmpver, *ver;
ret = (VertexSetClass*)ObjectDup(buf[0].od.ptr);
if (ObjectCheck( &buf[1], VertexClassID )) {
ver = &((VertexClass*)(buf[1].od.ptr))->ver;
} else if (ObjectCheck( &buf[1], VectorClassID )) {
Vector *vec;
memset(&tmpver, 0, sizeof(Vertex));
vec = &(((VectorClass*)buf[1].od.ptr)->vec);
tmpver.x = (short)vec->x;
tmpver.y = (short)vec->y;
tmpver.z = (short)vec->z;
ver = &tmpver;
} else {
ExecError( "論理演算の型が不正です(Vertex)" );
}
if (ident == OPE_PLUS || ident == OPE_OR) {
ret = VertexAppend(ret, ver);
} else {
ret = VertexDelete(ret, ver);
}
buf = StackAlloc( 1 );
buf->type = TYPE_OBJECT ;
buf->od.ptr = (Object*)ret ;
return RETURN_RETURN ;
}