/// /// \brief minimize starting from the initial guess x0 /// tstate operator()(const tproblem& problem, const tvector& x0) const { assert(problem.size() == static_cast<tsize>(x0.size())); // current state tstate cstate(problem, x0); // running-weighted-averaged-per-dimension-squared gradient average_vector_t<tscalar, tvector> gavg(x0.size()); for (tsize e = 0, k = 1; e < base_t::m_epochs; e ++) { for (tsize i = 0; i < base_t::m_epoch_size; i ++, k ++) { // learning rate const tscalar alpha = base_t::m_alpha0; // descent direction gavg.update(cstate.g.array().square(), base_t::weight(k)); cstate.d = -cstate.g.array() / (base_t::m_epsilon + gavg.value().array()).sqrt(); // update solution cstate.update(problem, alpha); } base_t::ulog(cstate); } return cstate; }
// Pass interpolation datapoints as tvectors. void spline::vectors (tvector<nr_double_t> y, tvector<nr_double_t> t) { int i = t.size (); assert (y.size () == i && i >= 3); // create local copy of f(x) realloc (i); for (i = 0; i <= n; i++) { f0[i] = y_(i); x[i] = t_(i); } }
void KickPlayer(class CCommand* pCommand, tvector<tstring>& asTokens, const tstring& sCommand) { if (!asTokens.size()) return; GameNetwork()->DisconnectClient(stoi(asTokens[0])); }
void CAOGenerator::RenderSetupSceneNode(CConversionSceneNode* pNode, tvector<tvector<float>>& aaflVerts) { if (!pNode) return; for (size_t c = 0; c < pNode->GetNumChildren(); c++) RenderSetupSceneNode(pNode->GetChild(c), aaflVerts); for (size_t m = 0; m < pNode->GetNumMeshInstances(); m++) { CConversionMeshInstance* pMeshInstance = pNode->GetMeshInstance(m); CConversionMesh* pMesh = pMeshInstance->GetMesh(); for (size_t f = 0; f < pMesh->GetNumFaces(); f++) { CConversionFace* pFace = pMesh->GetFace(f); size_t iMaterial = pMeshInstance->GetMappedMaterial(pFace->m)->m_iMaterial; while (aaflVerts.size() <= iMaterial) aaflVerts.push_back(); CConversionVertex* pVertex0 = pFace->GetVertex(0); for (size_t k = 2; k < pFace->GetNumVertices(); k++) { CConversionVertex* pVertex1 = pFace->GetVertex(k-1); CConversionVertex* pVertex2 = pFace->GetVertex(k); AddRenderedVertex(aaflVerts[iMaterial], pMeshInstance->GetVertex(pVertex0->v), pMesh->GetUV(pVertex0->vu)); AddRenderedVertex(aaflVerts[iMaterial], pMeshInstance->GetVertex(pVertex1->v), pMesh->GetUV(pVertex1->vu)); AddRenderedVertex(aaflVerts[iMaterial], pMeshInstance->GetVertex(pVertex2->v), pMesh->GetUV(pVertex2->vu)); } } } }
size_t FindEar(const tvector<vec3>& avecPoints) { size_t iPoints = avecPoints.size(); // A triangle is always an ear. if (iPoints <= 3) return 0; size_t i; vec3 vecFaceNormal; // Calculate the face normal. for (i = 0; i < iPoints; i++) { size_t iNext = (i+1)%iPoints; vec3 vecPoint = avecPoints[i]; vec3 vecNextPoint = avecPoints[iNext]; vecFaceNormal.x += (vecPoint.y - vecNextPoint.y) * (vecPoint.z + vecNextPoint.z); vecFaceNormal.y += (vecPoint.z - vecNextPoint.z) * (vecPoint.x + vecNextPoint.x); vecFaceNormal.z += (vecPoint.x - vecNextPoint.x) * (vecPoint.y + vecNextPoint.y); } vecFaceNormal.Normalize(); for (i = 0; i < iPoints; i++) { size_t iLast = i==0?iPoints-1:i-1; size_t iNext = i==iPoints-1?0:i+1; vec3 vecLast = avecPoints[iLast]; vec3 vecThis = avecPoints[i]; vec3 vecNext = avecPoints[iNext]; // Concave ones can not be ears. if ((vecLast-vecThis).Cross(vecLast-vecNext).Dot(vecFaceNormal) < 0) continue; bool bFoundPoint = false; for (size_t j = 0; j < iPoints; j++) { if (j == i || j == iLast || j == iNext) continue; if (PointInTriangle(avecPoints[j], vecLast, vecThis, vecNext)) { bFoundPoint = true; break; } } if (!bFoundPoint) return i; } return 0; }
bool CModel::LoadSourceFile() { #ifdef TINKER_NO_TOOLS return false; #else CConversionScene* pScene = new CConversionScene(); CModelConverter c(pScene); if (!c.ReadModel(m_sFilename)) { delete pScene; return false; } g_asTextures.clear(); g_aaflData.clear(); g_aabbVisBounds = AABB(Vector(999, 999, 999), Vector(-999, -999, -999)); g_aabbPhysBounds = AABB(Vector(999, 999, 999), Vector(-999, -999, -999)); LoadSceneIntoToy(pScene); size_t iTextures = g_asTextures.size(); m_ahMaterials.resize(iTextures); m_aiVertexBuffers.resize(iTextures); m_aiVertexBufferSizes.resize(iTextures); for (size_t i = 0; i < iTextures; i++) { if (g_aaflData[i].size() == 0) continue; m_aiVertexBuffers[i] = CRenderer::LoadVertexDataIntoGL(g_aaflData[i].size()*4, &g_aaflData[i][0]); m_aiVertexBufferSizes[i] = g_aaflData[i].size()/FIXED_FLOATS_PER_VERTEX; CData oMaterialData; CData* pShader = oMaterialData.AddChild("Shader", "model"); pShader->AddChild("DiffuseTexture", g_asTextures[i]); m_ahMaterials[i] = CMaterialLibrary::AddMaterial(&oMaterialData); //TAssert(m_aiMaterials[i]); if (!m_ahMaterials[i].IsValid()) TError(tstring("Couldn't create fake material for texture \"") + g_asTextures[i] + "\"\n"); } m_aabbVisBoundingBox = g_aabbVisBounds; m_aabbPhysBoundingBox = g_aabbPhysBounds; delete pScene; return true; #endif }
void SetCVar(CCommand* pCommand, tvector<tstring>& asTokens, const tstring& sCommand) { CVar* pCVar = dynamic_cast<CVar*>(pCommand); TAssert(pCVar); if (!pCVar) return; if (asTokens.size() > 1) pCVar->SetValue(asTokens[1]); TMsg(sprintf(tstring("%s = %s\n"), pCVar->GetName().c_str(), pCVar->GetValue().c_str())); }
tvector<nr_type_t> operator * (tvector<nr_type_t> a, tmatrix<nr_type_t> b) { assert (a.size () == b.getRows ()); int r, c, n = b.getRows (); nr_type_t z; tvector<nr_type_t> res (n); for (c = 0; c < n; c++) { for (r = 0, z = 0; r < n; r++) z += a.get (r) * b.get (r, c); res.set (c, z); } return res; }
tvector<itemType>::tvector(const tvector<itemType> & vec) // postcondition: vector is a copy of vec : mySize(vec.size()), myCapacity(vec.capacity()), myList(new itemType[myCapacity]) { int k; // copy elements for(k = 0; k < mySize; k++){ myList[k] = vec.myList[k]; } }
void CGameServer::DestroyAllEntities(const tvector<tstring>& asSpare, bool bRemakeGame) { if (!GameNetwork()->IsHost() && !IsLoading()) return; if (m_pWorkListener) m_pWorkListener->SetAction("Locating dead nodes", GameServer()->GetMaxEntities()); for (size_t i = 0; i < GameServer()->GetMaxEntities(); i++) { CBaseEntity* pEntity = CBaseEntity::GetEntity(i); if (!pEntity) continue; bool bSpare = false; for (size_t j = 0; j < asSpare.size(); j++) { if (asSpare[j] == pEntity->GetClassName()) { bSpare = true; break; } } if (bSpare) continue; pEntity->Delete(); if (m_pWorkListener) m_pWorkListener->WorkProgress(i); } if (m_pWorkListener) m_pWorkListener->SetAction("Clearing buffers", GameServer()->m_ahDeletedEntities.size()); for (size_t i = 0; i < GameServer()->m_ahDeletedEntities.size(); i++) { delete GameServer()->m_ahDeletedEntities[i]; if (m_pWorkListener) m_pWorkListener->WorkProgress(i); } GameServer()->m_ahDeletedEntities.clear(); if (CBaseEntity::GetNumEntities() == 0) CBaseEntity::s_iNextEntityListIndex = 0; if (bRemakeGame && GameNetwork()->IsHost()) m_hGame = CreateGame(); }
float DistanceToPolygon(const vec3& p, tvector<vec3>& v, vec3 n) { float flPlaneDistance = DistanceToPlane(p, v[0], n); size_t i; bool bFoundPoint = false; for (i = 0; i < v.size()-2; i++) { if (PointInTriangle(p, v[0], v[i+1], v[i+2])) { bFoundPoint = true; break; } } if (bFoundPoint) return flPlaneDistance; float flClosestPoint = -1; for (i = 0; i < v.size(); i++) { float flPointDistance = (v[i] - p).Length(); if (flClosestPoint == -1 || (flPointDistance < flClosestPoint)) flClosestPoint = flPointDistance; float flLineDistance; if (i == v.size() - 1) flLineDistance = DistanceToLineSegment(p, v[i], v[0]); else flLineDistance = DistanceToLineSegment(p, v[i], v[i+1]); if (flClosestPoint == -1 || (flLineDistance < flClosestPoint)) flClosestPoint = flLineDistance; } return flClosestPoint; }
void LoadMeshInstanceIntoToy(CConversionScene* pScene, CConversionMeshInstance* pMeshInstance, const Matrix4x4& mParentTransformations) { if (!pMeshInstance->IsVisible()) return; CConversionMesh* pMesh = pMeshInstance->GetMesh(); for (size_t m = 0; m < pScene->GetNumMaterials(); m++) { for (size_t j = 0; j < pMesh->GetNumFaces(); j++) { size_t k; CConversionFace* pFace = pMesh->GetFace(j); if (pFace->m == ~0) continue; CConversionMaterial* pMaterial = NULL; CConversionMaterialMap* pConversionMaterialMap = pMeshInstance->GetMappedMaterial(pFace->m); if (!pConversionMaterialMap) continue; if (!pConversionMaterialMap->IsVisible()) continue; if (pConversionMaterialMap->m_iMaterial != m) continue; while (g_asTextures.size() <= pConversionMaterialMap->m_iMaterial) { g_asTextures.push_back(pScene->GetMaterial(pConversionMaterialMap->m_iMaterial)->GetDiffuseTexture()); g_aaflData.push_back(); } size_t iMaterial = pConversionMaterialMap->m_iMaterial; CConversionVertex* pVertex0 = pFace->GetVertex(0); for (k = 2; k < pFace->GetNumVertices(); k++) { CConversionVertex* pVertex1 = pFace->GetVertex(k-1); CConversionVertex* pVertex2 = pFace->GetVertex(k); AddVertex(iMaterial, mParentTransformations * pMesh->GetVertex(pVertex0->v), pMesh->GetUV(pVertex0->vu)); AddVertex(iMaterial, mParentTransformations * pMesh->GetVertex(pVertex1->v), pMesh->GetUV(pVertex1->vu)); AddVertex(iMaterial, mParentTransformations * pMesh->GetVertex(pVertex2->v), pMesh->GetUV(pVertex2->vu)); } } } }
void FireInput(class CCommand* pCommand, tvector<tstring>& asTokens, const tstring& sCommand) { if (!CVar::GetCVarBool("cheats")) return; if (asTokens.size() < 3) { TMsg("Format: ent_input entityname input [optional args]\n"); return; } tvector<CBaseEntity*> apEntities; CBaseEntity::FindEntitiesByName(asTokens[1], apEntities); if (!apEntities.size()) { if (CVar::GetCVarBool("debug_entity_outputs")) TMsg("Console -> none\n"); else TError("No entities found that match name \"" + asTokens[1] + "\".\n"); return; } tstring sArgs; for (size_t i = 3; i < asTokens.size(); i++) sArgs += asTokens[i] + " "; for (size_t i = 0; i < apEntities.size(); i++) { CBaseEntity* pTargetEntity = apEntities[i]; if (CVar::GetCVarBool("debug_entity_outputs")) TMsg("Console -> " + tstring(pTargetEntity->GetClassName()) + "(\"" + pTargetEntity->GetName() + "\")." + asTokens[2] + "(\"" + sArgs + "\")\n"); pTargetEntity->CallInput(asTokens[2], sArgs); } }
bool ConvexHullIntersectsAABB(const AABB& box, const tvector<vec3>& avecPoints, const tvector<size_t>& aiTriangles) { TAssert(aiTriangles.size() % 3 == 0); vec3 vecCenter = box.Center(); vec3 n; for (size_t i = 0; i < aiTriangles.size(); i += 3) { const vec3& v1 = avecPoints[aiTriangles[i]]; const vec3& v2 = avecPoints[aiTriangles[i+1]]; const vec3& v3 = avecPoints[aiTriangles[i+2]]; n = (v2-v1).Cross(v3-v1).Normalized(); if (n.Dot(vecCenter-v1) < 0) continue; if (!TriangleIntersectsAABB(box, v1, v2, v3)) return false; } return true; }
void CApplication::ProcessJoystickInput() { if (g_aJoysticks.size() != MAX_JOYSTICKS) return; for (size_t i = 0; i < MAX_JOYSTICKS; i++) { CJoystick& oJoystick = g_aJoysticks[i]; if (!oJoystick.m_bPresent) continue; static tvector<float> aflAxis; aflAxis.resize(oJoystick.m_aflAxis.size()); glfwGetJoystickPos(i, &aflAxis[0], oJoystick.m_aflAxis.size()); for (size_t j = 0; j < oJoystick.m_aflAxis.size(); j++) { if (aflAxis[j] != oJoystick.m_aflAxis[j]) JoystickAxis(i, j, aflAxis[j], aflAxis[j]-oJoystick.m_aflAxis[j]); } oJoystick.m_aflAxis = aflAxis; static tvector<unsigned char> aiButtons; aiButtons.resize(oJoystick.m_iButtons); glfwGetJoystickButtons(i, &aiButtons[0], oJoystick.m_iButtons); for (size_t j = 0; j < oJoystick.m_iButtons; j++) { unsigned long long iButtonMask = (1<<j); if (aiButtons[j] == GLFW_PRESS && !(oJoystick.m_aiButtonStates&iButtonMask)) JoystickButtonPress(i, MapJoystickKey(j)); else if (aiButtons[j] == GLFW_RELEASE && (oJoystick.m_aiButtonStates&iButtonMask)) JoystickButtonRelease(i, MapJoystickKey(j)); if (aiButtons[j] == GLFW_PRESS) oJoystick.m_aiButtonStates |= iButtonMask; else oJoystick.m_aiButtonStates &= ~iButtonMask; } } }
const tvector<itemType> & tvector<itemType>::operator = (const tvector<itemType> & rhs) // postcondition: normal assignment via copying has been performed; // if vector and rhs were different sizes, vector // has been resized to match the size of rhs { if (this != &rhs) // don't assign to self! { delete [] myList; // get rid of old storage myCapacity = rhs.capacity(); mySize = rhs.size(); myList = new itemType [myCapacity]; // allocate new storage // copy rhs int k; for(k=0; k < mySize; k++) { myList[k] = rhs.myList[k]; } } return *this; // permit a = b = c = d }
bool CModel::Load(class CConversionScene* pScene, size_t iMesh) { g_aaflData.clear(); LoadMesh(pScene, iMesh); size_t iMaterials = g_aaflData.size(); for (size_t i = 0; i < iMaterials; i++) { if (g_aaflData[i].size() == 0) continue; m_aiVertexBuffers.push_back(CRenderer::LoadVertexDataIntoGL(g_aaflData[i].size()*4, &g_aaflData[i][0])); m_aiVertexBufferSizes.push_back(g_aaflData[i].size()/FIXED_FLOATS_PER_VERTEX); } m_aabbVisBoundingBox = g_aabbVisBounds; m_aabbPhysBoundingBox = g_aabbPhysBounds; return true; }