glm::dvec4 StdMaterial::directBrdf( const LightCast& lightCast, const Raycast& eyeRay) const { glm::dvec3 pos = eyeRay.origin + eyeRay.direction * eyeRay.limit; double scatt = scattering(pos); if(scatt <= 0.0) return glm::dvec4(0.0); double diffuseLightSize = lightCast.diffuseSize( lightCast, eyeRay, Raycast::getEntropy(scatt)); if(scatt >= 1.0) { return glm::dvec4(diffuseLightSize); } else { double lightDotEye = glm::dot(lightCast.raycast.direction, eyeRay.direction); double directness = (1 + lightDotEye) / 2.0; double lobe = 1.0 / scatt - 1.0; double power = cellar::fast_pow(directness, lobe); return glm::dvec4(power * diffuseLightSize); } }
void StdMaterial::scatterLight( std::vector<Raycast>& raycasts, const Raycast& ray) const { double scatt = scattering(ray.origin); glm::dvec4 scatterSample(color::white, 1.0); glm::dvec3 scatterPoint = ray.origin + ray.direction * ray.limit; if(scatt <= 0.0) { const glm::dvec3& direction = ray.direction; raycasts.push_back(Raycast( Raycast::FULLY_SPECULAR, scatterSample, scatterPoint, direction)); } else if(scatt >= 1.0) { glm::dvec3 direction = _sphereRand.gen(1.0); raycasts.push_back(Raycast( Raycast::FULLY_DIFFUSE, scatterSample, scatterPoint, direction)); } else { glm::dvec3 direction = _sphereRand.gen(1.0); direction = glm::mix(ray.direction, direction, scatt); direction = glm::normalize(direction); raycasts.push_back(Raycast( Raycast::getEntropy(scatt), scatterSample, scatterPoint, direction)); } }
int main(int argc,char** argv) { // Anzahl an Schritten unsigned long numSteps = DEFAULT_STEP_COUNT; // Überprüfen, ob die Anzahl an Schritten als Argument übergeben wurde, wenn ja, Variable updaten if (argc!=2) printf("Keine Schrittanzahl übergeben, verwende Standardwert von 1.000.000 ...\n"); else { numSteps = strtoul(argv[1],NULL,0); printf("Anzahl an Integrationsschritten: %lu\n",numSteps); } // Integrationsbereich double xl[] = { 0.,0. }; double xu[] = { 1.,1. }; // LHAPDF initialisieren LHAPDF::PDF* pdf = LHAPDF::mkPDF("MSTW2008lo90cl",0); // Monte-Carlo anlegen MonteCarlo mc(2,numSteps); // Funktion setzeeen MonteCarloFunction mcFunction(diffCrossSection,2,xl,xu,pdf); mc.setIntegrationFunction(&mcFunction); // Integrieren double result,error; //mc.integrate(&result,&error); // Ergebnis + Fehler ausgeben printf("cross section: %f +/- %f\n",result,error); // Alternativer Weg über Phasenraum und Matrixelement, dazu Streuprozess-Objekt erstellen TwoTwoOneFinalMassiveHadronScattering scattering(M_H,S_PP,M_H,pdf); // Dem Prozess beitragende einzelne Parton-Prozess registrieren (jeweils Quark-Antiquark) for (int i=1;i<7;i++) { scattering.addPartonProcess(i,-i,matrixElement); scattering.addPartonProcess(-i,i,matrixElement); } // Histogramme für die Verteilungen nach Rapidität und Transversalimpuls anlegen PhaseSpaceMonteCarloFunction transverseTransformFunction(scattering.getPhaseSpace(),transverseMomentum,NULL); scattering.addHistogram("transverseMomentum","Transverse Momentum",50,0.,1000.,&transverseTransformFunction); PhaseSpaceMonteCarloFunction rapidityTransformFunction(scattering.getPhaseSpace(),rapidity,NULL); scattering.addHistogram("rapidity","rapidity",50,-5.,5.,&rapidityTransformFunction); // Integrieren und Ergebnis ausgeben scattering.getCrossSection(&result,&error,numSteps); printf("cross section: %f +/- %f\n",result,error); // Histogramme zu Root-Histogrammen konvertieren Histogram* transverseMomentumHistogram = scattering.getHistogram("transverseMomentum"); Histogram* rapidityHistogram = scattering.getHistogram("rapidity"); TH1D* transverseRootHist = transverseMomentumHistogram->getRootTH1D(); TH1D* rapidityRootHist = rapidityHistogram->getRootTH1D(); // Histogramm in Root-File abspeichern TFile* file = new TFile("hist.root","RECREATE"); transverseRootHist->Write(); rapidityRootHist->Write(); file->Close(); // Speicher freigeben delete transverseRootHist; delete rapidityRootHist; delete file; return 0; }
Spectrum VolumeRegion::extinction(const Point3D &p, const Length3D &w, double time) const { return absorption(p, w, time) + scattering(p, w, time); }
void PeaksWorkspace::saveNexus(::NeXus::File * file) const { //Number of Peaks const size_t np(peaks.size()); // Column vectors for peaks table std::vector<int> detectorID(np); std::vector<double> H(np); std::vector<double> K(np); std::vector<double> L(np); std::vector<double> intensity(np); std::vector<double> sigmaIntensity(np); std::vector<double> binCount(np); std::vector<double> initialEnergy(np); std::vector<double> finalEnergy(np); std::vector<double> waveLength(np); std::vector<double> scattering(np); std::vector<double> dSpacing(np); std::vector<double> TOF(np); std::vector<int> runNumber(np); std::vector<double> goniometerMatrix(9 * np); // Populate column vectors from Peak Workspace for (size_t i = 0; i < np; i++) { Peak p = peaks[i]; detectorID[i] = p.getDetectorID(); H[i] = p.getH(); K[i] = p.getK(); L[i] = p.getL(); intensity[i] = p.getIntensity(); sigmaIntensity[i] = p.getSigmaIntensity(); binCount[i] = p.getBinCount(); initialEnergy[i] = p.getInitialEnergy(); finalEnergy[i] = p.getFinalEnergy(); waveLength[i] = p.getWavelength(); scattering[i] = p.getScattering(); dSpacing[i] = p.getDSpacing(); TOF[i] = p.getTOF(); runNumber[i] = p.getRunNumber(); { Matrix<double> gm = p.getGoniometerMatrix(); goniometerMatrix[9 * i] = gm[0][0]; goniometerMatrix[9 * i + 1] = gm[1][0]; goniometerMatrix[9 * i + 2] = gm[2][0]; goniometerMatrix[9 * i + 3] = gm[0][1]; goniometerMatrix[9 * i + 4] = gm[1][1]; goniometerMatrix[9 * i + 5] = gm[2][1]; goniometerMatrix[9 * i + 6] = gm[0][2]; goniometerMatrix[9 * i + 7] = gm[1][2]; goniometerMatrix[9 * i + 8] = gm[1][2]; } // etc. } // Start Peaks Workspace in Nexus File std::string specifyInteger = "An integer"; std::string specifyDouble = "A double"; file->makeGroup("peaks_workspace", "NXentry", true); // For when peaksWorkspace can be loaded // Detectors column file->writeData("column_1", detectorID); file->openData("column_1"); file->putAttr("name", "Dectector ID"); file->putAttr("interpret_as", specifyInteger); file->putAttr("units", "Not known"); file->closeData(); // H column file->writeData("column_2", H); file->openData("column_2"); file->putAttr("name", "H"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // K column file->writeData("column_3", K); file->openData("column_3"); file->putAttr("name", "K"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // L column file->writeData("column_4", L); file->openData("column_4"); file->putAttr("name", "L"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Intensity column file->writeData("column_5", intensity); file->openData("column_5"); file->putAttr("name", "Intensity"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Sigma Intensity column file->writeData("column_6", sigmaIntensity); file->openData("column_6"); file->putAttr("name", "Sigma Intensity"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Bin Count column file->writeData("column_7", binCount); file->openData("column_7"); file->putAttr("name", "Bin Count"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Initial Energy column file->writeData("column_8", initialEnergy); file->openData("column_8"); file->putAttr("name", "Initial Energy"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Final Energy column file->writeData("column_9", finalEnergy); file->openData("column_9"); file->putAttr("name", "Final Energy"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Wave Length Column file->writeData("column_10", waveLength); file->openData("column_10"); file->putAttr("name", "Wave Length"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Scattering Column file->writeData("column_11", scattering); file->openData("column_11"); file->putAttr("name", "Scattering"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // D Spacing Column file->writeData("column_12", dSpacing); file->openData("column_12"); file->putAttr("name", "D Spacing"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // TOF Column file->writeData("column_13", TOF); file->openData("column_13"); file->putAttr("name", "TOF"); file->putAttr("interpret_as", specifyDouble); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); //Run Number column file->writeData("column_14", runNumber); file->openData("column_14"); file->putAttr("name", "Run Number"); file->putAttr("interpret_as", specifyInteger); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // Goniometer Matrix Column std::vector<int> array_dims; array_dims.push_back(static_cast<int>(peaks.size())); array_dims.push_back(9); file->writeData("column_15", goniometerMatrix, array_dims); file->openData("column_15"); file->putAttr("name", "Goniometer Matrix"); file->putAttr("interpret_as", "A matrix of 3x3 doubles"); file->putAttr("units", "Not known"); // Units may need changing when known file->closeData(); // QLab & QSample are calculated and do not need to be saved file->closeGroup(); // end of peaks workpace }
int main( int argc, char **argv ) { //setup optimization variables std::string opt_env= "OSG_OPTIMIZER=COMBINE_ADJACENT_LODS SHARE_DUPLICATE_STATE MERGE_GEOMETRY MAKE_FAST_GEOMETRY CHECK_GEOMETRY OPTIMIZE_TEXTURE_SETTINGS STATIC_OBJECT_DETECTION"; #ifdef WIN32 _putenv(opt_env.c_str()); #else char * writable = new char[opt_env.size() + 1]; std::copy(opt_env.begin(), opt_env.end(), writable); writable[opt_env.size()] = '\0'; // don't forget the terminating 0 putenv(writable); delete[] writable; #endif const bool enableShadows = true; // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); // construct the viewer. osgViewer::Viewer viewer(arguments); // add the stats handler viewer.addEventHandler(new osgViewer::StatsHandler); viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator; keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() ); keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() ); keyswitchManipulator->addMatrixManipulator( '3', "Drive", new osgGA::DriveManipulator() ); keyswitchManipulator->addMatrixManipulator( '4', "Terrain", new osgGA::TerrainManipulator() ); keyswitchManipulator->addMatrixManipulator( '5', "Orbit", new osgGA::OrbitManipulator() ); keyswitchManipulator->addMatrixManipulator( '6', "FirstPerson", new osgGA::FirstPersonManipulator() ); keyswitchManipulator->addMatrixManipulator( '7', "Spherical", new osgGA::SphericalManipulator() ); viewer.setCameraManipulator( keyswitchManipulator.get() ); //Add sample data path osgDB::Registry::instance()->getDataFilePathList().push_back("../data"); osgDB::Registry::instance()->getDataFilePathList().push_back("./data"); //hack to be able to runt from GCC out dir osg::DisplaySettings::instance()->setNumMultiSamples(8); //Load terrain osg::ref_ptr<osg::Node> terrain = osgDB::readNodeFile("lz.osg"); if(!terrain) { std::cerr << "Terrain mesh not found\n"; return 0; } osg::Group* group = new osg::Group; group->addChild(terrain); //Create mesh LODs osgVegetation::MeshLODVector lods; lods.push_back(osgVegetation::MeshLOD("trees/fir01_l0.osg",50)); lods.push_back(osgVegetation::MeshLOD("trees/fir01_l1.osg",200)); //Create one mesh layers with LODS osgVegetation::MeshLayer spruce(lods); spruce.Density = 0.1; spruce.Height.set(0.5,0.5); spruce.Width.set(0.5,0.5); spruce.Scale.set(0.8,0.9); spruce.ColorIntensity.set(3.0,3.0); spruce.TerrainColorRatio = 1.0; spruce.UseTerrainIntensity = false; spruce.CoverageMaterials.push_back(WOODS); //Create mesh data that hold all mesh layers osgVegetation::MeshData tree_data; tree_data.ReceiveShadows = enableShadows; //Add layers tree_data.Layers.push_back(spruce); osg::ComputeBoundsVisitor cbv; terrain->accept(cbv); osg::BoundingBoxd bb(cbv.getBoundingBox()._min, cbv.getBoundingBox()._max); //osg::Vec3d bb_size = bb._max - bb._min; //Down size bb for faster generation...useful for testing purpose //bb._min = bb._min + bb_size*0.3; //bb._max = bb._max - bb_size*0.3; osg::Node* tree_node = NULL; //Create coverage data used by the terrain query class osgVegetation::CoverageData cd; //...add the materials, here we match material name with colors cd.CoverageMaterials.push_back(osgVegetation::CoverageData::CoverageMaterial(GRASS,osgVegetation::CoverageColor(0,0,1,1))); cd.CoverageMaterials.push_back(osgVegetation::CoverageData::CoverageMaterial(WOODS,osgVegetation::CoverageColor(1,1,1,1))); cd.CoverageMaterials.push_back(osgVegetation::CoverageData::CoverageMaterial(ROAD,osgVegetation::CoverageColor(0,0,1,1))); cd.CoverageMaterials.push_back(osgVegetation::CoverageData::CoverageMaterial(DIRT,osgVegetation::CoverageColor(1,0,0,1))); //Create terrain query class and by feeding terrain and coverage data osgVegetation::TerrainQuery tq(terrain.get(),cd); //create scattering class osgVegetation::EnvironmentSettings env_settings; osgVegetation::MeshQuadTreeScattering scattering(&tq,env_settings); try{ //Start generation tree_node = scattering.generate(bb,tree_data); group->addChild(tree_node); } catch(std::exception& e) { std::cerr << e.what(); return 0; } //Add light and shadows osg::Light* pLight = new osg::Light; pLight->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); osg::Vec4 lightPos(1,0.5,1,0); pLight->setPosition(lightPos); // last param w = 0.0 directional light (direction) osg::Vec3f lightDir(-lightPos.x(),-lightPos.y(),-lightPos.z()); lightDir.normalize(); pLight->setDirection(lightDir); pLight->setAmbient(osg::Vec4(0.3f, 0.3f, 0.3f, 1.0f) ); //pLight->setDiffuse(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f) ); osg::LightSource* pLightSource = new osg::LightSource; pLightSource->setLight( pLight ); group->addChild( pLightSource ); static int ReceivesShadowTraversalMask = 0x1; static int CastsShadowTraversalMask = 0x2; osg::ref_ptr<osgShadow::ShadowedScene> shadowedScene = new osgShadow::ShadowedScene; osgShadow::ShadowSettings* settings = shadowedScene->getShadowSettings(); settings->setReceivesShadowTraversalMask(ReceivesShadowTraversalMask); settings->setCastsShadowTraversalMask(CastsShadowTraversalMask); settings->setShadowMapProjectionHint(osgShadow::ShadowSettings::PERSPECTIVE_SHADOW_MAP); unsigned int unit=2; settings->setBaseShadowTextureUnit(unit); double n=0.8; settings->setMinimumShadowMapNearFarRatio(n); unsigned int numShadowMaps = 2; settings->setNumShadowMapsPerLight(numShadowMaps); int mapres = 1024; settings->setTextureSize(osg::Vec2s(mapres,mapres)); //settings->setShaderHint(osgShadow::ShadowSettings::PROVIDE_VERTEX_AND_FRAGMENT_SHADER); osg::ref_ptr<osgShadow::ViewDependentShadowMap> vdsm = new osgShadow::ViewDependentShadowMap; shadowedScene->setShadowTechnique(vdsm.get()); terrain->setNodeMask(ReceivesShadowTraversalMask); tree_node->setNodeMask(CastsShadowTraversalMask | ReceivesShadowTraversalMask); if(enableShadows) { shadowedScene->addChild(group); viewer.setSceneData(shadowedScene); } else { viewer.setSceneData(group); } return viewer.run(); }