⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 povray.cpp

📁 此代码经过大量使用
💻 CPP
字号:
/***************************************************************************                          povray.cpp  -  description                             -------------------    begin                : Sat Dec 8 2001    copyright            : (C) 2001 by Rudiger Koch    email                : rkoch@rkoch.org ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/#include "amygdala/povray.h"#include "amygdala/neuron.h"#include "amygdala/network.h"#include "amygdala/node.h"#include <string.h>#include <time.h>Povray::Povray(int ID, string headline, string dir){    Node *node = Node::GetNodeRef();    net = node->instance_begin()->second->GetNetwork();    OutFile(ID, headline, dir);}Povray::Povray(Network *_net, int ID, string headline, string dir=""){    net = _net;    OutFile(ID, headline, dir);}Povray::~Povray(){    fprintf(povfile, "    translate <0, 0, 0>\n}\n");    fprintf(povfile, "object { NN rotate <0, 0, 0> translate <0, 0, 0> }\n");    fclose(povfile);}void Povray::mkPovHeader(int Id){    fprintf(povfile, "#include \"colors.inc\"\n");    fprintf(povfile, "light_source { <-80, 0, -150> color White}\n");    fprintf(povfile, "light_source {  <40, -80, -150> color White}\n");//    fprintf(povfile, "light_source {  <40, 80, -150> color White}\n");//    fprintf(povfile, "light_source {  <0, 20, -150> color White}\n");    fprintf(povfile, "camera { location <0, 0, -15> look_at <0, 0, 0>}\n");    fprintf(povfile, "global_settings { ambient_light White }\n");    fprintf(povfile, "background { color rgb <0, 0, 0> }\n\n");    time_t T = time(NULL);    char * currentTime =  ctime(&T);    currentTime[strlen(currentTime)-1] = '\0';    fprintf(povfile, "text { ttf \"timrom.ttf\" \"Amygdala Spiking Network\" 0.1, 0 pigment { MidnightBlue } finish { reflection .25 specular .3 } translate <-25, 17, 25> }\n");    fprintf(povfile, "text { ttf \"timrom.ttf\" \"Pacman Id: %d\" 0.1, 0 pigment { MidnightBlue } finish { reflection .25 specular .3 } translate <-25, 15, 25> }\n", Id);    fprintf(povfile, "text { ttf \"timrom.ttf\" \"%s\" 0.1, 0 pigment { MidnightBlue } finish { reflection .25 specular .3 } translate <-25, 14, 25> }\n", currentTime);    fprintf(povfile, "#declare NN = union \n{\n");    fprintf(povfile, "    cylinder { <0, 0, 0>, <0, 0, 8>, 0.05  texture { pigment { color NavyBlue } }}\n");    fprintf(povfile, "    cylinder { <0, 0, 0>, <8, 0, 0>, 0.05  texture { pigment { color NavyBlue } }}\n");    fprintf(povfile, "    cylinder { <0, 0, 0>, <0, 8, 0>, 0.05  texture { pigment { color NavyBlue } }}\n\n");    fprintf(povfile, "//    box { <0,0,5>, <100,100, 6> texture { pigment { color Yellow } } }\n");}void Povray::WriteNetwork(){    if(Neuron::GetMPMode()) WriteMpNetwork();    if(!net) throw string("No Network Instance available");    Network::const_iterator pathItr = net->begin();    while (pathItr != net->end()) {        Neuron *currentNeuron = pathItr->second;        WriteNeuron(currentNeuron);        for (int i=0; i<currentNeuron->GetAxonSize(); i++) {            AmIdInt postId = currentNeuron->GetAxonID(i);            Neuron *postSynNeuron = net->GetNeuron(postId);            WriteSynapse(currentNeuron, postSynNeuron);        }        pathItr++;    }}void Povray::WriteTree(Neuron* rootNeuron, int depth){    depth--;    float currentLoc[3];    rootNeuron->GetPhysicalProperties()->GetLocation(currentLoc);    WriteNeuron(rootNeuron);    for (int i=0; i<rootNeuron->GetAxonSize(); i++) {        AmIdInt postId = rootNeuron->GetAxonID(i);        Neuron *postSynNeuron = net->GetNeuron(postId);        WriteSynapse(rootNeuron, postSynNeuron);        if(depth > 0) {            WriteTree(postSynNeuron, depth);    // recurse        } else {            WriteNeuron(postSynNeuron);        }    }}void Povray::WriteNeuron(Neuron *theNeuron){    float currentLoc[3];    theNeuron->GetPhysicalProperties()->GetLocation(currentLoc);    switch (theNeuron->GetLayerType()){        case INPUTLAYER:           fprintf(povfile, "    sphere { <%f, %f, %f>, 0.2 texture { pigment { color Blue } } }\n",                   currentLoc[0], currentLoc[1], currentLoc[2]);           break;        case OUTPUTLAYER:           fprintf(povfile, "    sphere { <%f, %f, %f>, 0.2 texture { pigment { color Red } } }\n",                   currentLoc[0], currentLoc[1], currentLoc[2]);           break;        default:           fprintf(povfile, "    sphere { <%f, %f, %f>, 0.08 texture { pigment { color Gray25 } } }\n",                   currentLoc[0], currentLoc[1], currentLoc[2]);           break;    }}void Povray::WriteSynapse(Neuron *pre, Neuron *post){    float preLoc[3];    float postLoc[3];    pre->GetPhysicalProperties()->GetLocation(preLoc);    post->GetPhysicalProperties()->GetLocation(postLoc);    AmIdInt preId = pre->GetID();    AmIdInt postId = post->GetID();    float distance = sqrt((preLoc[0]-postLoc[0]) * (preLoc[0]-postLoc[0]) +                          (preLoc[1]-postLoc[1]) * (preLoc[1]-postLoc[1]));    if(distance < 1E-4) return; // Povray gives an error if base point == apex    const char *color = "Cyan";    fprintf(povfile, "    cylinder { <%f, %f, %f>, <%f, %f, %f>, 0.01 texture { pigment { color %s } } } // %ld->%ld\n",                          preLoc[0], preLoc[1], preLoc[2],                          postLoc[0], postLoc[1], postLoc[2],                          color, preId, postId);    // now make a red head so we can destinguish between pre- and post synaptic neurons    float hx = postLoc[0] - (postLoc[0] - preLoc[0])/5.;    float hy = postLoc[1] - (postLoc[1] - preLoc[1])/5.;    float hz = postLoc[2] - (postLoc[2] - preLoc[2])/5.;    fprintf(povfile, "    cylinder { <%f, %f, %f>, <%f, %f, %f>, 0.02 texture { pigment { color %s } } } // %ld->%ld\n",                          hx, hy, hz,                          postLoc[0], postLoc[1], postLoc[2],                          "Red", preId, postId);}void Povray::OutFile(int ID, string headline, string dir){    if(dir.length() > 0) dir += "/";    char idString[20];    sprintf(idString, "%d", ID);    string filename(dir + string("id-") + string(idString) + string(".pov"));    povfile = fopen(filename.c_str(), "w");    if(!povfile) throw string("cannot open the output file");    mkPovHeader(ID);}void Povray::WriteMpNetwork(){    Node *node = Node::GetNodeRef();    for(hash_map <AmIdInt, Instance*>::iterator netItr = node->instance_begin();                netItr != node->instance_end(); netItr++){        net = netItr->second->GetNetwork();        Network::const_iterator pathItr = net->begin();        while (pathItr != net->end()) {            Neuron *currentNeuron = pathItr->second;            WriteNeuron(currentNeuron);            for (int i=0; i<currentNeuron->GetAxonSize(); i++) {                AmIdInt postId = currentNeuron->GetAxonID(i);                Neuron *postSynNeuron = net->GetNeuron(postId);                WriteSynapse(currentNeuron, postSynNeuron);            }            for (vector <SMPSynapse*>::iterator smpAxonItr = currentNeuron->smpaxon_begin();                    smpAxonItr != currentNeuron->smpaxon_end(); smpAxonItr++) {                Neuron *postSynNeuron = (*smpAxonItr)->GetPostNeuron();                WriteSynapse(currentNeuron, postSynNeuron);            }            pathItr++;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -