示例#1
0
SEXP updateFaceNormals(SEXP vb_, SEXP it_) {
  try {
    NumericMatrix vb(vb_);
    IntegerMatrix it(it_);
    mat vbA(vb.begin(),vb.nrow(),vb.ncol());
    mat normals(it.nrow(), it.ncol()); normals.fill(0.0);
    imat itA(it.begin(),it.nrow(),it.ncol());
    int nit = it.ncol();
    colvec tmp0(3), tmp1(3), ntmp(3);
    for (int i=0; i < nit; ++i) {
      tmp0 = vbA.col(itA(1,i))-vbA.col(itA(0,i));
      tmp1 = vbA.col(itA(2,i))-vbA.col(itA(0,i));
    
      crosspArma(tmp0,tmp1,ntmp);
      double nlen = norm(ntmp,2);
      if (nlen > 0)
	ntmp /= nlen; 
      normals.col(i) = ntmp;
    
    }
    return Rcpp::wrap(normals);
  } catch (std::exception& e) {
    ::Rf_error( e.what());
  } catch (...) {
    ::Rf_error("unknown exception");
  }
}
示例#2
0
文件: thunk.cpp 项目: KGE-INC/modplus
void *VDJITAllocator::Allocate(size_t len) {
	len = (len + 15) & ~(size_t)15;

	FreeChunks::iterator itMark(mNextChunk), itEnd(mFreeChunks.end()), it(itMark);

	if (it == itEnd)
		it = mFreeChunks.begin();

	for(;;) {
		for(; it!=itEnd; ++it) {
			if (it->second >= len) {
				it->second -= len;

				void *p = (char *)it->first + it->second;

				if (!it->second) {
					if (mNextChunk == it)
						++mNextChunk;

					mFreeChunks.erase(it);
				}

				return p;
			}
		}

		if (itEnd == itMark)
			break;

		it = mFreeChunks.begin();
		itEnd = itMark;
	}

	size_t alloclen = (len + mAllocationGranularity - 1) & ~(mAllocationGranularity - 1);

	void *p = VirtualAlloc(NULL, alloclen, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
	if (p) {
		try {
			Allocations::iterator itA(mAllocations.insert(Allocations::value_type(p, alloclen)).first);

			try {
				if (len < alloclen)
					mFreeChunks.insert(FreeChunks::value_type((char *)p + len, alloclen - len));

			} catch(...) {
				mAllocations.erase(itA);
				throw;
			}
		} catch(...) {
			VirtualFree(p, 0, MEM_RELEASE);
			p = NULL;
		}
	}

	return p;
}
void cCommands::loadACLs( void )
{
	// make sure it's clean
	QMap< QString, cAcl* >::iterator itA (_acls.begin());
	for ( ; itA != _acls.end(); ++itA )
		delete itA.data();
	_acls.clear();

	QStringList ScriptSections = DefManager->getSections( WPDT_PRIVLEVEL );

	if (ScriptSections.isEmpty()) {
		Console::instance()->log(LOG_WARNING, "No ACLs for players, counselors, gms and admins defined!\n");
		return;
	}

	// We are iterating trough a list of ACLs
	// In each loop we create one acl
	for( QStringList::iterator it = ScriptSections.begin(); it != ScriptSections.end(); ++it )
	{
		const cElement *Tag = DefManager->getDefinition( WPDT_PRIVLEVEL, *it );

		if( !Tag )
			continue;

		QString ACLname = *it;

		// While we are in this loop we are building an ACL
		cAcl *acl = new cAcl;
		acl->name = ACLname;
		acl->rank = Tag->getAttribute("rank", "1").toUShort();
		if (acl->rank == 0 || acl->rank == 255) {
			acl->rank = 1;
		}

		QMap< QString, bool > group;
		QCString groupName;


		for( unsigned int i = 0; i < Tag->childCount(); ++i )
		{
			const cElement *childTag = Tag->getChild( i );
			if( childTag->name() == "group" )
			{
				groupName = childTag->getAttribute( "name" );

				for( unsigned int j = 0; j < childTag->childCount(); ++j )
				{
					const cElement *groupTag = childTag->getChild( j );

					if( groupTag->name() == "action" )
					{
						QString name = groupTag->getAttribute( "name", "any" );
						bool permit = groupTag->getAttribute( "permit", "false" ) == "true" ? true : false;
						group.insert( name, permit );
					}
				}

				if( !group.isEmpty() )
				{
					acl->groups.insert( groupName, group );
					group.clear();
				}
			}
		}

		_acls.insert( ACLname, acl );
	}

	// Renew the ACL pointer for all loaded accounts
	Accounts::instance()->clearAcls();
}
示例#4
0
/* 
 * Implementation of Kronecker product. 
 * Eigen has an implementation of the Kronecker product,
 * but it is very slow due to poor memory reservation.
 * See: https://forum.kde.org/viewtopic.php?f=74&t=106955&p=309990&hilit=kronecker#p309990
 * When Eigen update their implementation, and officially support it, we switch to that.
 */
SparseMatrix myKroneckerProduct(const SparseMatrix &A, const SparseMatrix &B)
{
    SparseMatrix AB(A.rows()*B.rows(), A.cols()*B.cols());

    // Reserve memory for AB

    //AB.reserve(A.nonZeros()*B.nonZeros()); // Does not reserve inner vectors (slow)
    //int innernnz = std::ceil(A.nonZeros()*B.nonZeros()/AB.outerSize());
    //AB.reserve(Eigen::VectorXi::Constant(AB.outerSize(), innernnz)); // Assumes equal distribution of non-zeros (slow)

    // Calculate exact number of non-zeros for each inner vector
    Eigen::VectorXi nnzA = Eigen::VectorXi::Zero(A.outerSize());
    Eigen::VectorXi nnzB = Eigen::VectorXi::Zero(B.outerSize());
    Eigen::VectorXi nnzAB = Eigen::VectorXi::Zero(AB.outerSize());
    //innerNonZeros.setZero();

    for (int jA = 0; jA < A.outerSize(); ++jA)
    {
        int nnz = 0;
        for (SparseMatrix::InnerIterator itA(A,jA); itA; ++itA) nnz++;
        nnzA(jA) = nnz;
    }

    for (int jB = 0; jB < B.outerSize(); ++jB)
    {
        int nnz = 0;
        for (SparseMatrix::InnerIterator itB(B,jB); itB; ++itB) nnz++;
        nnzB(jB) = nnz;
    }

    int innz = 0;
    for (int i = 0; i < nnzA.rows(); ++i)
    {
        for (int j = 0; j < nnzB.rows(); ++j)
        {
            nnzAB(innz) = nnzA(i)*nnzB(j);
            innz++;
        }
    }

    AB.reserve(nnzAB);

    // Non-zero tolerance
    double tolerance = std::numeric_limits<SparseMatrix::Scalar>::epsilon();

    // Compute Kronecker product
    for (int jA = 0; jA < A.outerSize(); ++jA)
    {
        for (SparseMatrix::InnerIterator itA(A,jA); itA; ++itA)
        {
            if (std::abs(itA.value()) > tolerance)
            {
                int jrow = itA.row()*B.rows();
                int jcol = itA.col()*B.cols();

                for (int jB = 0; jB < B.outerSize(); ++jB)
                {
                    for (SparseMatrix::InnerIterator itB(B,jB); itB; ++itB)
                    {
                        if (std::abs(itA.value()*itB.value()) > tolerance)
                        {
                            int row = jrow + itB.row();
                            int col = jcol + itB.col();
                            AB.insert(row,col) = itA.value()*itB.value();
                        }
                    }
                }
            }
        }
    }
    AB.makeCompressed();
    return AB;
}
void cCommands::loadACLs( void )
{
	// make sure it's clean
	QMap< QString, cAcl* >::iterator itA (_acls.begin());
	for ( ; itA != _acls.end(); ++itA )
		delete itA.data();
	_acls.clear();

	QStringList ScriptSections = DefManager->getSections( WPDT_PRIVLEVEL );
	
	if( ScriptSections.isEmpty() )
	{
		clConsole.ChangeColor( WPC_RED );
		clConsole.send( tr("WARNING: No ACLs for players, counselors, gms and admins defined!\n") );
		clConsole.ChangeColor( WPC_NORMAL );
		return;
	}

	// We are iterating trough a list of ACLs
	// In each loop we create one acl
	for( QStringList::iterator it = ScriptSections.begin(); it != ScriptSections.end(); ++it )
	{
		const QDomElement *Tag = DefManager->getSection( WPDT_PRIVLEVEL, *it );

		if( Tag->isNull() )
			continue;

		QString ACLname = Tag->attribute("id");

		if ( ACLname == QString::null )
		{
			clConsole.ChangeColor( WPC_RED );
			clConsole.send( tr("WARNING: Tag %1 lacks \"id\" attribute").arg(Tag->tagName()) );
			clConsole.ChangeColor( WPC_NORMAL );
			continue;
		}
		
		// While we are in this loop we are building an ACL
		cAcl *acl = new cAcl;
		acl->name = ACLname;
		QMap< QString, bool > group;
		QString groupName;

		QDomNode childNode = Tag->firstChild();
		while( !childNode.isNull() )
		{
			if( childNode.isElement() )
			{
				QDomElement childTag = childNode.toElement();
				if( childTag.nodeName() == "group" )
				{
					groupName = childTag.attribute("name", QString::null);
					QDomNode chchildNode = childTag.firstChild();
					while( !chchildNode.isNull() )
					{
						if( chchildNode.isElement() )
						{
							QDomElement chchildTag = chchildNode.toElement();
							if( chchildTag.nodeName() == "action" )
							{
								QString name = chchildTag.attribute( "name", "any" );
								bool permit = chchildTag.attribute( "permit", "false" ) == "true" ? true : false;
								group.insert( name, permit );
							}
						}
						chchildNode = chchildNode.nextSibling();
					}

					if( !group.isEmpty() )
					{
						acl->groups.insert( groupName, group );
						group.clear();
					}
				}
			}
			childNode = childNode.nextSibling();			
		}

		_acls.insert( ACLname, acl );	
	}
	DefManager->unload( WPDT_PRIVLEVEL );
}
示例#6
0
SEXP updateVertexNormals(SEXP vb_, SEXP it_,SEXP angweight_) {
  try {
    typedef unsigned int uint;
    bool angweight = Rcpp::as<bool>(angweight_);
    NumericMatrix vb(vb_);
    IntegerMatrix it(it_);
    mat vbA(vb.begin(),vb.nrow(),vb.ncol());
    mat normals = vbA*0;
    imat itA(it.begin(),it.nrow(),it.ncol());
    //setup vectors to store temporary data
    colvec tmp0(3), tmp1(3), tmp2(3), angtmp(3), ntmp(3);
    int nit = it.ncol();
    for (int i=0; i < nit; ++i) {
      tmp0 = vbA.col(itA(1,i))-vbA.col(itA(0,i));
      tmp1 = vbA.col(itA(2,i))-vbA.col(itA(0,i));
      if (angweight) {
	tmp2 = vbA.col(itA(1,i))-vbA.col(itA(2,i));
	angtmp(0) = angcalcArma(tmp0,tmp1);
	angtmp(1) = angcalcArma(tmp0, tmp2);
	angtmp(2) = angcalcArma(-tmp1, tmp2);
      }
      crosspArma(tmp0,tmp1,ntmp);
      for (int j=0; j < 3; ++j) {
	double co = dot(normals.col(itA(j,i)),ntmp);
      
	if (co < 0)  {
	  if (!angweight) {
	    normals.col(itA(j,i)) -= ntmp;
	  } else {
	    normals.col(itA(j,i)) -= ntmp*angtmp(j);
	  }
	} else {
	  if (! angweight) {
	    normals.col(itA(j,i)) += ntmp;
	  } else {
	    normals.col(itA(j,i)) += ntmp*angtmp(j);
	  }
	}
      }
    }
    for (uint i=0; i < normals.n_cols; ++i) {
      double nlen = norm(normals.col(i),2);
      if (nlen > 0)
	normals.col(i) /= nlen;
    }
		       
    return Rcpp::wrap(normals);
  } catch (std::exception& e) {
    ::Rf_error( e.what());
  } catch (...) {
    ::Rf_error("unknown exception");
  }
}